Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago

REST API Post data connection to Power BI

Hi Team,

I want to connect REST API Post data to Power BI. I am able to generate the access token but not able to populate the data using POST method. I am referring below document

https://chris.koester.io/index.php/2015/07/16/get-data-from-twitter-api-with-power-query/#comment-235

What would be the changes require in M query to generate the data using POST method( in above ref. link GET method used)

All help would be appreciated.

 

Regards,

Shailesh

2 Replies

  • v-kkf-msft's avatar
    v-kkf-msft
    Community Support

    Hi Anonymous ,

     

    Web.Contents can take a second optional record argument.  Here is the syntax (with each of the record fields being optional):

     

    Web.Contents("http://myurl.com",
    
    [ ApiKeyName = "Bacon",
    Content = "SomeHexOrText",
    Query = [ Data1 = "A", Data2 = "B" ]])

    As for those three record parameters, here is what they do:

    Query: This parameter allows you to programmatically add query parameters to the URL without having to worry about escaping.  In the example above, the URL would be appended with ?Data1=A&Data2=B.
    ApiKeyName: 
    If your target site has some notion of an API key, this parameter allows you to specify the name (not the value) of the key parameter that must be used in the URL.  You specify the value of the Api key using the "Web API" credential option, so that you can send your document to someone else, and they would be able to use their own API key.  Technically, you can use the Query parameter to effect the same behavior, but then the API key and value would travel with the document.
    Content: Specifying this value changes the web request from a GET to a POST, using the value of the Content field as the content of the POST.

     

    The POST request is included in the document you share. https://chris.koester.io/index.php/2015/07/16/.... 

    Generating a User Access Token:

          POST: https://api.twitter.com/oauth/request_token

    let
     // Concatenates the Consumer Key & Consumer Secret and converts to base64
     authKey = "Basic " & Binary.ToText(Text.ToBinary("<ConsumerKey>:<ConsumerSecret>"),0),
     url = "https://api.twitter.com/oauth2/token",
     // Uses the Twitter POST oauth2/token method to obtain a bearer token
     GetJson = Web.Contents(url,
         [
             Headers = [#"Authorization"=authKey,
                        #"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"],
             Content = Text.ToBinary("grant_type=client_credentials") 
         ]
     ),
     FormatAsJson = Json.Document(GetJson),
     // Gets token from the Json response
     in
       FormatAsJson[access_token]

     

     

    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.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you Winniz for the reply.

      I am able to generate access token but not able performed next step. In the reference link, GET method has mentioned once access token has generated. In my my case i have to use POST method for access token as well as for data. 

      When I used query mentioned in the reference link,  getting below error:

      Regards,

      Shailesh