Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
5kancho
Frequent Visitor

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.

5kancho_0-1670493866687.png

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.

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

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.

View solution in original post

2 REPLIES 2
5kancho
Frequent Visitor

Thanks, @v-jingzhang ! I used your second suggested approach and it works well.

v-jingzhang
Community Support
Community Support

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.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.