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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

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
Community Champion
Community Champion

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
Community Champion
Community Champion

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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.