cancel
Showing results for 
Search instead for 
Did you mean: 
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

Accepted Solutions
Super User III
Super User III

Re: RestApi with Session Authentication

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...

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

Imke Feldmann

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

1 REPLY 1
Super User III
Super User III

Re: RestApi with Session Authentication

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...

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

Imke Feldmann

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

Helpful resources

Announcements
New Ranks Launched March 24th!

New Ranks Launched March 24th!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power BI Community!

‘Better Together’ Contest Finalists Announced!

‘Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

Arun 'Triple A' Event Video, Q&A, and Slides

Arun 'Triple A' Event Video, Q&A, and Slides

Missed the Arun 'Triple A' event or want to revisit it? We've got you covered! Check out the video, Q&A, and slides now.

Top Solution Authors
Top Kudoed Authors