Forum Discussion
How to Authenticate to a REST API with basic Authentication in Power BI Blank Query
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.
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"
}]
}
- Youssef9 years agoMicrosoft 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.
- f_z3 years agoNew Member
Has there been any updates to this? I'm trying to connect a REST API (using POST) to PowerBI with basic authentication.
- lahdo9 years agoNew Member
This is what works for me:
Source = Json.Document(Web.Contents(address,
[
Headers = [#"Content-Type"="application/json", #"Authorization"="Basic asdasdasdSDASDADasdasd"]
] )),- hugoberry9 years agoResponsive Resident
Here is a POST request I used for dropbox API call
let data = [ path= "/code", recursive=false, include_media_info=false, include_deleted=false, include_has_explicit_shared_members=false], header = [ #"Authorization"="Bearer ZZZZZZZZZZZ", #"Content-Type"= "application/json"], response = Web.Contents("https://api.dropboxapi.com/2/files/list_folder",[Content=Json.FromValue(data),Headers=header]), out = Json.Document(response,1252) in out- hugoberry9 years agoResponsive Resident
As for why your particular request worked I believe is that because of how your service API is configured.
#"Authorization"="Basic <encoded username : password>"
The approach that Youssef was mentioning also should have worked, but I know that usualy this implies that some portions of your authentication is sent over to the service adrress in plain text, and your service might have restrictions on that. Another possibility is that your service's hashing algorithm for username:password might differe from that of Power BI.
By the way, I had problems making POST request via Web.Contents only when using Active Directroy. As long as you include some sort of authenticated token in the Header of your request, you should be fine.