Forum Discussion
Web.contents unbale to authenticate with anonymous authentication
Hey folks, I hope I can get some help with an issue I am facing.
I am retrieving data from an API that uses JWT token for authentication. So I have to make 2 requests:
1. To login and get the token.
//Get JSON Web Token via API
optionsToken = [#"Content-Type"="application/json; charset=UTF-8"],
bodyToken = "{""username"":""real_username"",""password"":""real_password""}",
responseToken = Web.Contents(urlApi & pathToken,[Headers = optionsToken,Content = Text.ToBinary(bodyToken)]),
//Get Access Token and Token Type
responseJson = Json.Document(responseToken),
// name of the token property
accessToken = responseJson[token]
2. Make the data request with the token
//Get Data
optionsData = [#"Content-Type"="application/json; charset=UTF-8",#"x-redlock-auth"=accessToken],
postData = "{""sample_post_data""}",
responseData = Web.Contents(
urlApi & pathData,
[
Headers = optionsData,
Content = Text.ToBinary(postData)
]
),
data = Json.Document(responseData)
The API requires using POST method so I need to use Anonymous authentication and format the headers and body properly with using the `Content` option.
All of that is working fine and I am able to get the data with the data call. But when I attempt to perform any transformations on the data I get the error "We couldn't authenticate with the credentials provided.
If I go into advanced editor and copy the code with the new transform step and use it in a newly openned document it works. This is the case for any subsequent step I attempt on the data.
Hi Anonymous
You have two data sources in the same query:
urlApi & pathTokenand
urlApi & pathDataI recommend that you use the common part urlApi as the basic url in Web.Contents(), then pass pathToken and pathData to RelativePath property. This may solve the problem.
Another method you can try is to paste the first part code (To login and get the token) into a new blank query, then convert this query into a custom function.
()=> let //Get JSON Web Token via API optionsToken = [#"Content-Type"="application/json; charset=UTF-8"], bodyToken = "{""username"":""real_username"",""password"":""real_password""}", responseToken = Web.Contents(urlApi & pathToken,[Headers = optionsToken,Content = Text.ToBinary(bodyToken)]), //Get Access Token and Token Type responseJson = Json.Document(responseToken), // name of the token property accessToken = responseJson[token] in accessTokenThen in your original query, call this custom function in the header to get access token.
#"x-redlock-auth"=getAccessToken()Hope this helps.
Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.
2 Replies
- v-jingzhang
Community Support
Hi Anonymous
You have two data sources in the same query:
urlApi & pathTokenand
urlApi & pathDataI recommend that you use the common part urlApi as the basic url in Web.Contents(), then pass pathToken and pathData to RelativePath property. This may solve the problem.
Another method you can try is to paste the first part code (To login and get the token) into a new blank query, then convert this query into a custom function.
()=> let //Get JSON Web Token via API optionsToken = [#"Content-Type"="application/json; charset=UTF-8"], bodyToken = "{""username"":""real_username"",""password"":""real_password""}", responseToken = Web.Contents(urlApi & pathToken,[Headers = optionsToken,Content = Text.ToBinary(bodyToken)]), //Get Access Token and Token Type responseJson = Json.Document(responseToken), // name of the token property accessToken = responseJson[token] in accessTokenThen in your original query, call this custom function in the header to get access token.
#"x-redlock-auth"=getAccessToken()Hope this helps.
Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it. - AnonymousNot applicable
Thanks, v-jingzhang ! I used your second suggested approach and it works well.