Forum Discussion

medwards807's avatar
medwards807
Icon for Helper I rankHelper I
9 years ago

How to Authenticate to a REST API with basic Authentication in Power BI Blank Query

I have a need to access a REST API (JSON) to access data in CSV format.

 

https://apiURL/apps/api/batch-query/

 

With Header as follows:

 

Authorization = "Basic <encoded username : password>"

Content-Type = "application/json"

 

My code is as follows:

 

let
 source = #!"Json.Document(Web.Contents(""https://apiURL.com/apps/api/batch- query/"", [#Authentication=""Basic <encoded username : password""]))"
in
 source

 

I receive error saying the header authorization can not be used. Most examples I have seen are using token/key, this API does not have that capability. Using a REST Client the call works just fine.

 

Is there a simple way to make a connection to the API with basic Auth, I need to do a POST, GET, GET (each requests will use a value from the previous request.

 

Thanks

10 Replies

  • Youssef's avatar
    Youssef
    Icon for Microsoft Employee rankMicrosoft Employee

    Power BI Desktop supports basic authentication out of the box.

     

    You can remove the authentication part in your Web.Contents call and just call your URL. Something like:

    Json.Document(Web.Contents("https://apiURL.com/apps/api/batch-query"))

     

    Power BI will prompt you to authenticate and will provide multiple options. Just pick the one called "Basic" and enter the username and password. That should automatically insert the base64 encoded username/password in the authentication header.

     

    Hope this helps.

    • medwards807's avatar
      medwards807
      Icon for Helper I rankHelper I

       

      Youssef,


      Thanks for the information, I gave it a try and it didn't work, when prompting for credentials and selecting basic and entering Username and Password it did not work. However I was able to get the following code to work.

       

      Note: I just had to set the Data Source to Anonymous

       

      let
         actualUrl = "https://apiurl.com/apps/api/batch-query/",
         options = [Headers =[#"Content-type"="application/json",
                    #"Authorization"="Basic <encoded username : password>"]],
         result = Web.Contents(actualUrl, options),
         #"Imported JSON" = Json.Document(result,1252)
      in
         #"Imported JSON"

       

      I would be interested in your opinion on this vs your original suggestion? or maybe some insight on how this works.

       

      In addition I need to make this a POST Request, in reading it sounds like I just need to add a [Content=Text.ToBinary(PostContents)])

       

      Where PostContents = JSON Text, can you provide any insight on how I would add the Post Content into my request?

       

      {
      "format" : "csv",
      "version" : "1.1",
      "name" : "Example",
      "encrypted" : "none",
      "useQueryLabels" : "true",
      "dateTimeUtc" : "true",
      "queries"  : [ {
          "name" : "Subscriptions",
          "query" : "select * from Subscription",
          "type" : "export"
       },
       {
       "name" : "Accounting",
          "query" : "select Id,StartDate,EndDate,FiscalYear,Name,Status from AccountingPeriod",
          "type" : "export"
         }]
      }

      • Youssef's avatar
        Youssef
        Icon for Microsoft Employee rankMicrosoft Employee

        The easiest way to know why the authentication didn't work is by using Fiddler to compare the requests made when you used the OOTB basic authentication vs. your workaround. The built-in basic auth should create this header for you and attach it to every request.

         

        Making Post requests in Power BI Desktop to APIs is not supported at the moment (if you use the Content option as you mentioned, you'll get a "Method Not Allowed" error). We are working on some Power BI Desktop extensibility capabilities that would enable you to do things like that and more. There is no exact ETA at the moment, but we are shooting for the first half of 2017.

         

        Hope this helps.

  • Anonymous's avatar
    Anonymous
    Not applicable

    I have a need to access a REST API (JSON) to access data in CSV format.

     

    GET https://fb.satmetrix.com/app/core/v1/feedback/1550421980492230

     

     

    I am getting an error when I tried submit basic authentication string. It shows authentication cannot be provided,Please check your credential 

     

    Is there a simple way to make a connection to the API with basic Auth, I need to do GET data

    • Mastok's avatar
      Mastok
      New Member

      Did you find a solution for this?