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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
dedelman_clng
Community Champion
Community Champion

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": "xxx@xxx.com",
    "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

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

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" .

vcgaomsft_1-1710815514465.png

vcgaomsft_2-1710815695992.png

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

View solution in original post

2 REPLIES 2
dedelman_clng
Community Champion
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

v-cgao-msft
Community Support
Community Support

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" .

vcgaomsft_1-1710815514465.png

vcgaomsft_2-1710815695992.png

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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