Forum Discussion
Issues with dynamic token retrieval "query. DataSource.Error: Web.Contents failed to get contents "
- Anonymous3 years ago
Hi Anonymous - you just need to get the syntax correct for Power BI to work, but it is difficult to follow with see what Postman is doing (with the sensitive information removed). Here is an example using Power BI REST API with Service Principal:
##This is the structure of the HTTP REST API Call POST https://login.windows.net/{#tenant id}/oauth2/token Content-Type: application/x-www-form-urlencoded grant_type=client_credentials&resource={#resource}&client_id={#client id}&client_secret={#client secret}Now look at the Power BI Version - This helpfully because you can see all the intermediate steps results before the APICall fails because there is no Client ID, Secret or Tenant. Note this works because of the Content Type requirements defined in the header, and the structure for bodystring including grant_type is often used.
##This is the Power BI Query Script let ClientID = "<clientid>", Secret = "<clientsecret>", TenantID = "<tenantid>", Resource = "<resource>", BaseURL = "https://login.windows.net/" & TenantID & "/oauth2/token", BodyString = "grant_type=client_credentials&resource=" & Resource & "&client_id=" & ClientID & "&client_secret=" & Secret, Body = Text.ToBinary(BodyString), APICall = Web.Contents( BaseURL, [ Headers = [#"Content-Type" = "application/x-www-form-urlencoded"], Content = Body ] ), OpenFile = Json.Document(APICall), GetToken = OpenFile[access_token] in GetTokenThe result is the Token to use in the next Web Call, which I believe you show this is working.
If your requires a Json file in the body, try something like this to preview the Json file before including in the body.let Source = Json.FromValue([username="<Useranme>", password="<Password>"]), #"Preview Json" = Text.FromBinary(Source) in #"Preview Json"
Hi Anonymous - you have not quite followed the example in the link provided. It is difficult to help properly without fully understanding the required API syntax. I have found it help to test the REST API syntax in VS Code using HTTP to really understand the expected structure and then translate to Power BI. Here are some thought on each image above:
Image 1 - you will normally find the that the Text from Binary is used with Json.FromValue - PowerQuery M | Microsoft Learn before provide into the Web.Content function. But I don't think it will be right to use the Content Type application/json. The Content Type in Image 3 is what I would expect an API of this nature.
Image 2 - I am not sure about this because you seem to supply a token to retrieve a token.
Image 3 - This appear to have the right content type to request a token but the body is all wrong. You should be trying to supply the grant_type=password&username=###&password=### as the previous Suppot Ticket suggests.
Image 4 - Authorisation is a different approach to previous images. The previous images would be POST API the content is submitted. Authorisation is Header use for GET and POST.
- Anonymous3 years agoNot applicable
Thank you for the response.
So I have only been sandboxing with the API in postman, and it works.
Maybe I should make it clear that the API requires Basic Auth in order for me to retrieve the Access token (that's why it looks like I use a token to get a token in img2, it's just the username and pw encoded), and in Postman I just add that information to the application (pw & username) so that is part of why I'm having a hard time translating how to do this..
So I use one URL to get the token, with basic auth, and then with that response I get the token. And with that token I can retrieve the data that I want. I'm not sure if I made anything clearer.