Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
diddlmus
Frequent Visitor

RestApi with Session Authentication

Hi everybody

 

I'm trying to get data from and Rest API, i am close to getting it to work, but the rest API requieres and SessionToken whick im able to pull out with the following query

 

 

let

    body = Text.ToBinary("{""Name"":""AX"", ""Key"":""XXXXXXXXXXXXXXX=""}"),
   url = "http://api.xxxxx.com/login/application",
   options = [
      Headers =[#"Content-type"="application/json"],
      Content=body
   ],
   result = Web.Contents(url, options),
   #"Converted to Table" = Xml.Tables(result,null,65001),
   access_token = #"Converted to Table"{0}[SessionToken]
in
access_token

which gives me a single text value  YYYYYYYYYYYYYYYY

 

 

I cant figure out how to pass this value into another query in Powerbi i have this Query to pull out data:

 

let
   url = "http://api.xxxxxx.com/employees/getemployeedetails/?token=YYYYYYYYYYYYYYYYYY&page=0",
   options = [Headers =[#"Content-type"="application/json"]],
   result = Web.Contents(url, options),
    #"Imported XML" = Xml.Tables(result,null,65001),
    Table = #"Imported XML"{0}[Table],
    #"Changed Type" = Table.TransformColumnTypes(Table,{{"CanAddOwnShiftsOnSmartphone", type logical}, {"CanApproveEmployeeChanges", type logical}, {"CanEditActualHours", type logical}, {"CanEditPlan", type logical}, {"CanSendSms", type logical}, {"CustomColumns", type text}, {"CustomRoles", type text}, {"Email", type text}, {"EmployeeId", type text}, {"EmployeeIsAdmin", type logical}, {"EmployeeType", type text}, {"EmployerNumber", Int64.Type}, {"EnforceMaxWeeklyWorkingHours", type logical}, {"HomeDepartment", type text}, {"IsUserEnabled", type logical}, {"Name", type text}, {"ReasonForTermination", type text}, {"SendShiftReminder", type logical}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Changed Type",{"Competencies", "IsUserEnabled", "Name", "WageNumber"}),
    #"Expanded WageNumber" = Table.ExpandTableColumn(#"Removed Other Columns", "WageNumber", {"Element:Text"}, {"WageNumber.Element:Text"}),
    #"Expanded Competencies" = Table.ExpandTableColumn(#"Expanded WageNumber", "Competencies", {"CompetenciesModel"}, {"Competencies.CompetenciesModel"}),
    #"Expanded Competencies.CompetenciesModel" = Table.ExpandTableColumn(#"Expanded Competencies", "Competencies.CompetenciesModel", {"Name", "Status"}, {"Competencies.CompetenciesModel.Name", "Competencies.CompetenciesModel.Status"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Competencies.CompetenciesModel", each ([Competencies.CompetenciesModel.Name] = "AX-BRUGER") and ([Competencies.CompetenciesModel.Status] = "true"))
in
    #"Filtered Rows"

if i insert the session token manuel in the query i get the required data, but i want Powerbi to do this dynamically. Is it even possible?

 

I have reat through numerous post about Rest API in PowerBI but havent got anything to work, så hope somebody can help me.

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Yes, you have to use the result of your function as a variable parameter in the url like so:

 

let

	MyToken = let

			body = Text.ToBinary("{""Name"":""AX"", ""Key"":""XXXXXXXXXXXXXXX=""}"),
			url = "http://api.xxxxx.com/login/application",
			options = [
			  Headers =[#"Content-type"="application/json"],
			  Content=body
			   ],
			result = Web.Contents(url, options),
			#"Converted to Table" = Xml.Tables(result,null,65001),
			access_token = #"Converted to Table"{0}[SessionToken]
		in
			access_token,

	url = "http://api.xxxxxx.com/employees/getemployeedetails/?token=" & MyToken & "&page=0",
	options = [Headers =[#"Content-type"="application/json"]],
	result = Web.Contents(url, options),
    #"Imported XML" = Xml.Tables(result,null,65001),
    Table = #"Imported XML"{0}[Table],
    #"Changed Type" = Table.TransformColumnTypes(Table,{{"CanAddOwnShiftsOnSmartphone", type logical}, {"CanApproveEmployeeChanges", type logical}, {"CanEditActualHours", type logical}, {"CanEditPlan", type logical}, {"CanSendSms", type logical}, {"CustomColumns", type text}, {"CustomRoles", type text}, {"Email", type text}, {"EmployeeId", type text}, {"EmployeeIsAdmin", type logical}, {"EmployeeType", type text}, {"EmployerNumber", Int64.Type}, {"EnforceMaxWeeklyWorkingHours", type logical}, {"HomeDepartment", type text}, {"IsUserEnabled", type logical}, {"Name", type text}, {"ReasonForTermination", type text}, {"SendShiftReminder", type logical}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Changed Type",{"Competencies", "IsUserEnabled", "Name", "WageNumber"}),
    #"Expanded WageNumber" = Table.ExpandTableColumn(#"Removed Other Columns", "WageNumber", {"Element:Text"}, {"WageNumber.Element:Text"}),
    #"Expanded Competencies" = Table.ExpandTableColumn(#"Expanded WageNumber", "Competencies", {"CompetenciesModel"}, {"Competencies.CompetenciesModel"}),
    #"Expanded Competencies.CompetenciesModel" = Table.ExpandTableColumn(#"Expanded Competencies", "Competencies.CompetenciesModel", {"Name", "Status"}, {"Competencies.CompetenciesModel.Name", "Competencies.CompetenciesModel.Status"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Competencies.CompetenciesModel", each ([Competencies.CompetenciesModel.Name] = "AX-BRUGER") and ([Competencies.CompetenciesModel.Status] = "true"))
in
    #"Filtered Rows"

If you need to refresh this in the service, you will hav to shift the variable part to a query-record like described here:

https://blog.crossjoin.co.uk/2016/08/23/web-contents-m-functions-and-dataset-refresh-errors-in-power...

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

2 REPLIES 2
ImkeF
Super User
Super User

Yes, you have to use the result of your function as a variable parameter in the url like so:

 

let

	MyToken = let

			body = Text.ToBinary("{""Name"":""AX"", ""Key"":""XXXXXXXXXXXXXXX=""}"),
			url = "http://api.xxxxx.com/login/application",
			options = [
			  Headers =[#"Content-type"="application/json"],
			  Content=body
			   ],
			result = Web.Contents(url, options),
			#"Converted to Table" = Xml.Tables(result,null,65001),
			access_token = #"Converted to Table"{0}[SessionToken]
		in
			access_token,

	url = "http://api.xxxxxx.com/employees/getemployeedetails/?token=" & MyToken & "&page=0",
	options = [Headers =[#"Content-type"="application/json"]],
	result = Web.Contents(url, options),
    #"Imported XML" = Xml.Tables(result,null,65001),
    Table = #"Imported XML"{0}[Table],
    #"Changed Type" = Table.TransformColumnTypes(Table,{{"CanAddOwnShiftsOnSmartphone", type logical}, {"CanApproveEmployeeChanges", type logical}, {"CanEditActualHours", type logical}, {"CanEditPlan", type logical}, {"CanSendSms", type logical}, {"CustomColumns", type text}, {"CustomRoles", type text}, {"Email", type text}, {"EmployeeId", type text}, {"EmployeeIsAdmin", type logical}, {"EmployeeType", type text}, {"EmployerNumber", Int64.Type}, {"EnforceMaxWeeklyWorkingHours", type logical}, {"HomeDepartment", type text}, {"IsUserEnabled", type logical}, {"Name", type text}, {"ReasonForTermination", type text}, {"SendShiftReminder", type logical}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Changed Type",{"Competencies", "IsUserEnabled", "Name", "WageNumber"}),
    #"Expanded WageNumber" = Table.ExpandTableColumn(#"Removed Other Columns", "WageNumber", {"Element:Text"}, {"WageNumber.Element:Text"}),
    #"Expanded Competencies" = Table.ExpandTableColumn(#"Expanded WageNumber", "Competencies", {"CompetenciesModel"}, {"Competencies.CompetenciesModel"}),
    #"Expanded Competencies.CompetenciesModel" = Table.ExpandTableColumn(#"Expanded Competencies", "Competencies.CompetenciesModel", {"Name", "Status"}, {"Competencies.CompetenciesModel.Name", "Competencies.CompetenciesModel.Status"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Competencies.CompetenciesModel", each ([Competencies.CompetenciesModel.Name] = "AX-BRUGER") and ([Competencies.CompetenciesModel.Status] = "true"))
in
    #"Filtered Rows"

If you need to refresh this in the service, you will hav to shift the variable part to a query-record like described here:

https://blog.crossjoin.co.uk/2016/08/23/web-contents-m-functions-and-dataset-refresh-errors-in-power...

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Thanks a lot for this code, in 2021 I was able to do a POST through Power Query with authentication.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.