Forum Discussion

dedelman_clng's avatar
dedelman_clng
Community Champion
2 years ago
Solved

help with POST api call

I am trying to replicate the API call which includes refresh of an access token as described here: https://community.fabric.microsoft.com/t5/Power-Query/Refresh-token-api-call/td-p/937553 

 

My API uses POST with "body" text instead of parameters. I have confirmed the POST call with the correct BODY parameters works thru Postman, but I can't seem to get the formatting correct in Power Query and am just getting "(400): Bad Request" from the line that makes the Web.Contents call. 

 

Here is the PQ code

let
    //App Details
    client_id = #"client_id", //<===parameter
    client_secret = #"client_secret", //<===parameter
    
    //Authentication
    
    //URL's
    token_uri = #"token_url", //<===parameter
    register_url = #"register_url", //<===parameter

    //body = Text.Combine({"{","""login_id""",":", """", client_id, """", ",", """security_key""", ":", """", client_secret,"""","}"}),
    body = Json.FromValue([login_id = client_id, security_key = client_secret]),
    header = [#"Content-Type"="application/json"],
    tokenAsk = Web.Contents("https://accounts.pyrus.com/api/v4/auth", [Headers = header, Content = body]),
   tokenResponse = Json.Document(tokenAsk),
    access_token = tokenResponse[access_token],
    token = Text.Combine({"Bearer ", tokenResponse[access_token]})
// GetJsonQuery = Web.Contents("https://api.pyrus.com/v4/forms/515736/register", [Headers=[#"Authorization"="token"]]),
//FormatAsJsonQuery = Json.Document(GetJsonQuery)
 
in
tokenResponse

As you can see I tried "body" in 2 different ways, and neither has worked.

 

The code snippet from Postman that works is (login/pw masked)

curl --location 'https://accounts.pyrus.com/api/v4/auth' \
--header 'Content-Type: application/json' \
--header 'Cookie: www=kuberg1' \
--data-raw '{
    "login": "[email protected]",
    "security_Key": "******"
    }'

(I don't know what kuberg1 as a cookie is - that was the default and I'm generally not a web api coder)

 

Can anyone see what I'm missing in my PowerQuery script?

 

Thanks,

David

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi David ,

    In your Postman request, you’re using "login" and "security_Key" as keys, but in your Power Query code, you’re using   "login_id" and "security_key" .

    so please verify that all required parameters are correctly provided and that there are no typos or case sensitivity issues with the parameter names.

    In Power Query, when you're constructing the body of a POST request, it's crucial to ensure that the body is a binary type because the function expects the option to be binary. So please check this:

    body = Text.ToBinary(Json.FromValue([login_id = client_id, security_key = client_secret])), 

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly--How to provide sample data in the Power BI Forum--China Power BI User Group

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi David ,

    In your Postman request, you’re using "login" and "security_Key" as keys, but in your Power Query code, you’re using   "login_id" and "security_key" .

    so please verify that all required parameters are correctly provided and that there are no typos or case sensitivity issues with the parameter names.

    In Power Query, when you're constructing the body of a POST request, it's crucial to ensure that the body is a binary type because the function expects the option to be binary. So please check this:

    body = Text.ToBinary(Json.FromValue([login_id = client_id, security_key = client_secret])), 

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly--How to provide sample data in the Power BI Forum--China Power BI User Group

  • dedelman_clng's avatar
    dedelman_clng
    Community Champion

    Hi Gao - 

     

    Thank you for the feedback. Postman and the API do not appear to be case-sensitive as I get the same result with "security_key" and "security_Key".

     

    As for the "body" variable, I changed it to this as suggested

     

     

     

        body = Text.ToBinary(Json.FromValue([login_id = client_id, security_key = client_secret])),

     

     

     

    but now I get "We cannot convert a value of type Binary to type Text" error message. What am I missing? The variables are defined as so

     

     

        client_id = #"client_id", //<===parameter
        client_secret = #"client_secret", //<===parameter
        
        token_uri = #"token_url", //<===parameter
        register_url = #"register_url", //<===parameter

     

     

    and the parameters are all type Text.

     

    ETA: The value for client_secret contains all alphanumeric characters except for a single dash ( - ). Could that special character be the cause of the error?

     

    Thanks,

    David