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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
viniciuscastilh
Frequent Visitor

Problem Api

Hi

I'm trying to pull the Token from Mercado Livre by Api inside Power Query, but without result I've already created a previous topic, but I thought it had resolved, but it hasn't been resolved, this is the code I'm using:

 

let
    api_url = "https://api.mercadolibre.com/",
    token_path = "oauth/token",
    ClientID = "---",
    ClientSecret = "---",
    code = "---",
    redirect_uri = "---",

    
EncodedCredentials = Binary.ToText(Text.ToBinary(ClientID & ":" & ClientSecret & ":" & code & ":" & redirect_uri), BinaryEncoding.Base64),
    
    Token_Response  = Json.Document(Web.Contents(api_url,
    [ 
      RelativePath = token_path,
      Headers = [#"Content-Type"="application/x-www-form-urlencoded",#"Authorization"=code],
      Content=Text.ToBinary("grant_type=authorization_code")
    ]
    )
    )
in

 



It displays the following error:

DataSource.Error: Web.Contents failed to get content from 'https://api.mercadolibre.com/oauth/token' (400): Bad Request
Detalhes:
DataSourceKind=Web
DataSourcePath=https://api.mercadolibre.com/oauth/token
Url=https://api.mercadolibre.com/oauth/token

Follows the material that the API provider makes available:

 

curl -X POST \
-H 'accept: application/json' \
-H 'content-type: application/x-www-form-urlencoded' \
'https://api.mercadolibre.com/oauth/token' \
-d 'grant_type=authorization_code' \
-d 'client_id=$APP_ID' \
-d 'client_secret=$SECRET_KEY' \
-d 'code=$SERVER_GENERATED_AUTHORIZATION_CODE' \
-d 'redirect_uri=$REDIRECT_URI'

 



Can someone help me with this code, I don't know what I can do to work

Thanks

Vinicius

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

instead of

Content=Text.ToBinary("grant_type=authorization_code")

you need to write 

Content=Text.ToBinary("grant_type=authorization_code,client_id=app_id,client_secret=key,code=auth,redirect_uri=redirect")

of course with the appropriate substitutions.

 

You can always test your API call at https://reqbin.com/ 

View solution in original post

3 REPLIES 3
lbendlin
Super User
Super User

instead of

Content=Text.ToBinary("grant_type=authorization_code")

you need to write 

Content=Text.ToBinary("grant_type=authorization_code,client_id=app_id,client_secret=key,code=auth,redirect_uri=redirect")

of course with the appropriate substitutions.

 

You can always test your API call at https://reqbin.com/ 

lbendlin
Super User
Super User

your Content parameter needs to contain all five key/value pairs (like the CURL call), not just grant_type.

 

(You have an EncodedCredentials step but you don't use the result anywhere)

Hi

I haven't been able to make it work yet, I'm suspicious that this way the API doesn't work, but I'm not managing to develop another one, this part is not my specialty

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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

Top Solution Authors
Top Kudoed Authors