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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
JoseJunior22
New Member

Power Query- M Language - Failure to Request a json service API -

Hi, people. I really need a helping hand, since i've been trying to solve this problem for one week, but it seems to be impossible to get it alone. The question is: this "M" code (below) makes a https url request to another endpoint for a second time (urlRequest = "https://api-sandbox.anbima.com.br/feed/precos-indices/v1/titulos-publicos/mercado-secundario-TPF") . It runs fine until the step which the M script receives the access_token: the authentication process is running well so far. Unfortunatelly, after this previous step, when i call a JSON service for the second time, the Power Query shows a box with the following message: "Specify how to connect". When I click the box buttom, Power Query shows me a window to choose the authentication method to allow the conection. Then, I select the "anonymous" option. After that, Power BI shows me, again,  another message box; "it's not possible to authenticate with the credencials".  My knowledgment about this environment stops here... The problem seems to be at the urlRequest object in the script, but I don't know how to fix it. Any help would be very apreciated! Thanks in advance, José. 

 

Here is the M code:

 

let
body = "{
""grant_type"" : ""client_credentials""
}",

minhaURL = "https://api.anbima.com.br/oauth/access-token",
getToken = Json.Document(Web.Contents(minhaURL,
[
Headers=[#"Content-Type"="application/json",
#"Authorization" = "Basic "&Binary.ToText(Text.ToBinary("Uw6QvcZwzRtp:qaRCeKStnXhE"), 0)
],
Content=Text.ToBinary(body)])
),

access_token = getToken[access_token],
authToken = "bearer " & access_token,

//urlRequest= path & "/feed/precos-indices/v1/titulos-publicos/curvas-juros",
urlRequest = "https://api-sandbox.anbima.com.br/feed/precos-indices/v1/titulos-publicos/mercado-secundario-TPF",
//keyPhrases = KeyPhrases(),
results = Json.Document(Web.Contents(
urlRequest,
[
Headers=[#"Content-Type"="application/json",
client_id = "Uw6QvcZwzRtp",
access_token = authToken
]
]
)
)
in results

 

 

The window below show the problem. The text is written in portuguese language.

 

JoseJunior22_0-1656524946883.png

 

 

1 ACCEPTED SOLUTION
v-kkf-msft
Community Support
Community Support

Hi @JoseJunior22 ,

 

This is because you are using an incorrect access token:

authToken = "bearer " & access_token

 

It is not necessary to prefix the token with "bearer". Please use the following code.

 

let
body = "{
""grant_type"" : ""client_credentials""
}",

minhaURL = "https://api.anbima.com.br/oauth/access-token",
getToken = Json.Document(Web.Contents(minhaURL,
[
Headers=[#"Content-Type"="application/json",
#"Authorization" = "Basic "&Binary.ToText(Text.ToBinary("Uw6QvcZwzRtp:qaRCeKStnXhE"), 0)
],
Content=Text.ToBinary(body)])
),

access_token = getToken[access_token],

//urlRequest= path & "/feed/precos-indices/v1/titulos-publicos/curvas-juros",
urlRequest = "https://api-sandbox.anbima.com.br/feed/precos-indices/v1/titulos-publicos/mercado-secundario-TPF",
//keyPhrases = KeyPhrases(),
results = Json.Document(Web.Contents(
urlRequest,
[
Headers=[#"Content-Type"="application/json",
client_id = "Uw6QvcZwzRtp",
access_token = access_token
]
]
)
)
in results

vkkfmsft_0-1656988420371.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-kkf-msft
Community Support
Community Support

Hi @JoseJunior22 ,

 

This is because you are using an incorrect access token:

authToken = "bearer " & access_token

 

It is not necessary to prefix the token with "bearer". Please use the following code.

 

let
body = "{
""grant_type"" : ""client_credentials""
}",

minhaURL = "https://api.anbima.com.br/oauth/access-token",
getToken = Json.Document(Web.Contents(minhaURL,
[
Headers=[#"Content-Type"="application/json",
#"Authorization" = "Basic "&Binary.ToText(Text.ToBinary("Uw6QvcZwzRtp:qaRCeKStnXhE"), 0)
],
Content=Text.ToBinary(body)])
),

access_token = getToken[access_token],

//urlRequest= path & "/feed/precos-indices/v1/titulos-publicos/curvas-juros",
urlRequest = "https://api-sandbox.anbima.com.br/feed/precos-indices/v1/titulos-publicos/mercado-secundario-TPF",
//keyPhrases = KeyPhrases(),
results = Json.Document(Web.Contents(
urlRequest,
[
Headers=[#"Content-Type"="application/json",
client_id = "Uw6QvcZwzRtp",
access_token = access_token
]
]
)
)
in results

vkkfmsft_0-1656988420371.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

otravers
Community Champion
Community Champion

It's probably because https://api-sandbox.anbima.com.br/ is returning an error. You need a URL that will properly resolve.

------------------------------------------------
1. How to get your question answered quickly - good questions get good answers!
2. Learning how to fish > being spoon-fed without active thinking.
3. Please accept as a solution posts that resolve your questions.
------------------------------------------------
BI Blog: Datamarts | RLS/OLS | Dev Tools | Languages | Aggregations | XMLA/APIs | Field Parameters | Custom Visuals

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors