Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
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.
Solved! Go to Solution.
Hi @5kancho
You have two data sources in the same query:
urlApi & pathToken
and
urlApi & pathData
I 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
accessToken
Then 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.
Thanks, @v-jingzhang ! I used your second suggested approach and it works well.
Hi @5kancho
You have two data sources in the same query:
urlApi & pathToken
and
urlApi & pathData
I 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
accessToken
Then 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.
Check out the July 2025 Power BI update to learn about new features.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
User | Count |
---|---|
10 | |
8 | |
7 | |
6 | |
6 |