Forum Discussion
Get token function for API request with dynamic access token
- 3 years ago
Hej :),
Here is a quick summary of steps to take:
1. Create static connection to your API using "Authorization" header and format it like "Bearer XXXXX" (It seems you have done this already
2. Create function for like this for querying your token.(I am connecting to to PBI rest API in this example: https://login.microsoftonline.com/{{tenant id}}/oauth2/token)
Here is an example of this:
() =>let
body = "grant_type=client_credentials&scope=https://analysis.windows.net/powerbi/api/.default&client_id="&client_id&"&client_secret="&client_secret&"&resource="&resource,
Data =Json.Document(Web.Contents("https://login.microsoftonline.com/"&tenant_id&"/oauth2/token",[Headers=[#"Content-Type"="application/x-www-form-urlencoded"],Content=Text.ToBinary(body)])),
access_token = Data[access_token]
in
access_token
Code explained:
1st We define what kind of body our request has
2nd connecting to data using web.contents. Here important thing is Headers3rd get token
Note: I am using parameters in my code for sensitive data. I recommend this for added security and dynamic options3. Replace your static token used in step 1 with the function name. E.g. DynamicToken()
Here is an exellent video about this where instead of service principal login (like in my case) they are using username and password: https://youtu.be/N8qYRSqRz84
I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/
I am sorry to return to this again, but I wonder if what I was requesting was misunderstood. So I want to import data to Power BI from a custom REST API. I just want to understand why I am using the microsoft URL?
- ValtteriN3 years agoCommunity Champion
Hi,
The microsoft url is just an example. You can replace it with one that suits your use case.- Anonymous3 years agoNot applicable
Yes but the confusion is as the two url are different. So I have one to extract the token, which is suppose to replace the example in the Data =.. declaration, but what should I replace the first one with (body)?