Forum Discussion

JoseJunior22's avatar
JoseJunior22
New Member
4 years ago
Solved

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.

 

 

 

  • 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

     

    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.

2 Replies

  • v-kkf-msft's avatar
    v-kkf-msft
    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

     

    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.