Forum Discussion

viniciuscastilh's avatar
viniciuscastilh
Frequent Visitor
5 years ago
Solved

API IN POWER BI POST

Good afternoon

I need to integrate the Mercado Livre api into Power BI, I need to make a Post inside the Body
But I never made a Post on Power Bi, could you help me?

The basis for calling the API is as follows:

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'


I tried to develop the following code, but still the Token still does not return as needed

 

 

let
body = "
{
""grant_type"": ""authorization_code"",
""client_id"": ""--"",
""client_secret"": ""--"",
""code"": ""--"",
""redirect_uri"": ""--""
}
",

getToken =
Json.Document(
Web.Contents (
"https://api.mercadolibre.com",
[
RelativePath = "/oauth/token",
Content=Text.ToBinary(body),
Headers = [
#"ContentType" = "application/json"
]
]
)
)
in body


Could you help me?



 

  • Hi viniciuscastilh 

     

    Does that cURL POST work?

     

    The general form for making a POST in PBI/PQ is

     

    Source = Json.Document(Web.Contents(url,[
    Headers = [#"Content-Type"="application/json"],
    Content = Text.ToBinary(body)
    ]
    ))

     

     

    In your particular case try this

     

    let
        api_url = "https://api.mercadolibre.com/",
        token_path = "oauth/token",
        ClientID = "xxxxxxxx",
        ClientSecret = "xxxxxxxx",
        
        EncodedCredentials = "Basic " & Binary.ToText(Text.ToBinary(ClientID & ":" & ClientSecret), BinaryEncoding.Base64),
        
        Token_Response  = Json.Document(Web.Contents(api_url,
        [ 
          RelativePath = token_path,
          Headers = [#"Content-Type"="application/x-www-form-urlencoded",#"Authorization"=EncodedCredentials],
          Content=Text.ToBinary("grant_type=authorization_code")
        ]
        )
        )
    in
        Token_Response

     

    Regards

    Phil

7 Replies

  • Hi viniciuscastilh 

     

    Does that cURL POST work?

     

    The general form for making a POST in PBI/PQ is

     

    Source = Json.Document(Web.Contents(url,[
    Headers = [#"Content-Type"="application/json"],
    Content = Text.ToBinary(body)
    ]
    ))

     

     

    In your particular case try this

     

    let
        api_url = "https://api.mercadolibre.com/",
        token_path = "oauth/token",
        ClientID = "xxxxxxxx",
        ClientSecret = "xxxxxxxx",
        
        EncodedCredentials = "Basic " & Binary.ToText(Text.ToBinary(ClientID & ":" & ClientSecret), BinaryEncoding.Base64),
        
        Token_Response  = Json.Document(Web.Contents(api_url,
        [ 
          RelativePath = token_path,
          Headers = [#"Content-Type"="application/x-www-form-urlencoded",#"Authorization"=EncodedCredentials],
          Content=Text.ToBinary("grant_type=authorization_code")
        ]
        )
        )
    in
        Token_Response

     

    Regards

    Phil

    • viniciuscastilh's avatar
      viniciuscastilh
      Frequent Visitor

      Thanks

      Perfect solution, I just had to make adaptations but it worked, thank you very much!

      Regards

      Vinicius

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Vinicius, I'm looking to do exactly the same thing that you are doing here. Could you please help me? I've done a registry of app on Meli and have the Id and Secret... but still can't connect...!!!

  • When you POST you normally push content to the server. In Power Query you do that by providing a value for the Content parameter (usually by packing it with JSON.FromValue() ).  That will automatically convert the GET call to a POST call.

     

    Web.Contents - PowerQuery M | Microsoft Docs