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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Markzolotoy
Impactful Individual
Impactful Individual

Get token from Web API

I need to get a token from a Web API. It is a POST with the following parameters:

grant_type=password&username=domainname\username&client_id=xxxxxxxxx

 

Any idea how to do it?

 

Thanks

10 REPLIES 10
Markzolotoy
Impactful Individual
Impactful Individual

Yes, I am doing something like this:

let
url = "https://localhost:446/token",
body = "{""grant_type"":""password"",
""username"":""domain\username"",
""client_id"":""xxxxxxx""
}",
Source = Json.Document(Web.Contents(url, [Headers=[#"Content-Type"="application/x-www-form-urlencoded"],
Content = Text.ToBinary(body)
]))

in
#"Source"

But I am getting error 400. That usually means bad parameters. I tried Fiddler, but sis not see that request in it. 

Again, it dependes on the API documentation. I would check running the POST request from Restman or Postman and confirme if the body is correct. The 404 can be from a wrong header up to a wrong body. So it depends.

For example, does it make sense the anwser with or without the Text.ToBinary()?

Text.ToBinary(body)

You can try outside power bi first and get to know the parameters really deep and then try to match the same way with Power Bi. 

Hope that help,


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Happy to help!

LaDataWeb Blog

Markzolotoy
Impactful Individual
Impactful Individual

Yes, I tried it in Postman. The actual problem (I was wrong about parameters at this point) is that the user has to be windows authenticated first. If I change credentials to Windows then I get:

Web.Contents with the Content option is only supported when connecting anonymously.

Markzolotoy
Impactful Individual
Impactful Individual

Again, the question IS: How to place a POST call with specific headers and a few paraameters from PBI report. Dont worry about returns and other things. Let's say token end point is: localhost://token.

So, do you know how to do it?

Alright, let me try to be generic. You can build a request like this in power query getting data from blank query:

= let
   body = "The POST method body here",
   Data= Web.Contents("https://yourusrlhere",[Content=Text.ToBinary(body),Headers=[#"Content- 
   Type"="application/json"]]),
   DataRecord = Json.Document(Data),
   Source=DataRecord 
in
   Source

 These posts might help
https://community.powerbi.com/t5/Desktop/Connect-to-a-Web-Service-sending-parameters/td-p/225510#

https://community.powerbi.com/t5/Desktop/How-to-run-POST-request-in-M/td-p/457138

Hope that helps


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Happy to help!

LaDataWeb Blog

Markzolotoy
Impactful Individual
Impactful Individual

It's all in my intial post:

I need to get a token from a Web API. It is a POST with the following parameters:

grant_type=password&username=domainname\username&client_id=xxxxxxxxx

 

That's not documentation that will help us help you my friend. An API doc will describe things like

- The main URL

- Describe how to authenticate

- If it need a token, describe which request helps you get it.

- List of request you can do (specifing method POST/GET and parameters)

- Definition of the response, format and fields.

These are some examples but it should include even more. Each API is different and there is no gold rule to connect. Without a documentation with specifications you won't be able to connect the data 😞

Regards,


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Happy to help!

LaDataWeb Blog

Markzolotoy
Impactful Individual
Impactful Individual

@ibarrau I am not sure I was asking about what you are talking about. I need to get data from endpoints that run on-prem. It is working fine if I use test versions without a token. So, if an endpoint looks like this:

computername://api/getTestData it works fine. In reality that endpoint fails if a token is not provided. It should work just like it works when the app is running on-prem where the front-end obtains a token first and then attaches it to every data request.

Alright if it's not Microsoft API you should check your API documentation. Every API comes with its documentation in order to learn how it authenticates or request your data.

We might help if you show the documentation, but be sure that it's not some private stuff from your company.

Regards,


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Happy to help!

LaDataWeb Blog

ibarrau
Super User
Super User

Hi. Assuming are you talking about the bearer token from microsoft because you haven't explained mored and considering you have already register the app. You can use this endpoint with these parameters

Endpoint: https://login.windows.net/common/oauth2/token/

Body: resource=https://analysis.windows.net/powerbi/api&client_id=[ApplicationId]&grant_type=password&username=[You...

If you haven't done anything I have said before. You can check this post from scratch to get a token and run a dataset refresh example:

https://blog.ladataweb.com.ar/post/188045227735/get-access-token

 

Hope that helps,


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Happy to help!

LaDataWeb Blog

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors