cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
mnayar
Solution Specialist
Solution Specialist

Power Rest API Authorization Token Error

Hello, I am trying to get an authroization token from Azure Service Principal using Postman and Power Query so I can query the Power BI Rest API but I keep getting an error. I cannot use a username and password to pull the data so we have set up a code and clientsecret. Has anyone had any experience setting it up? I have tried the configuration in POSTMAN and Power Quer

 

The URL I am using is: https://login.microsoftonline.com/<tenantid>/oauth2/authorize

Other parameters included in the body:

client_id

code

client_secret

grant_type: authorization_code

response_mode: query

resource: https://analysis.windows.net/powerbi/api

 

mnayar_0-1623696875023.png

 

I have also tried the following url  https://login.microsoftonline.com/<tenantid>/oauth2/token

and I get an invalid_grant error

mnayar_6-1623697591810.png

 

 

 

 

Other values set in the App Registeration are:

mnayar_1-1623697135299.png

mnayar_2-1623697179139.png

mnayar_3-1623697208268.png

 

I am using the following code in Power Query

mnayar_4-1623697369505.png

 

 

 

 

 

 

 

11 REPLIES 11
Watsky
Solution Supplier
Solution Supplier

Hey @mnayar  I haven't setup using authorization_code grant types before, but I have used client_secrets. Have you tried changing your grant_type to client_credentials? Try changing your grant type on https://login.microsoftonline.com/<tenantid>/oauth2/token

see if that works for you. 

 

If that doesn't work for you take a look at this info

 

Microsoft identity platform and OAuth 2.0 authorization code flow - Microsoft identity platform | Mi...

 

You see towards the middle of the page how to use authorization_code grant types but it appears to have different keys than you're currently using.


Did my answer(s) help you? Give it a kudos by clicking the Thumbs Up! ?
Did my post answer your question(s)? Mark my post as a solution. This will help others find the solution.

mnayar
Solution Specialist
Solution Specialist

Hello Watsky, When you mentioned grant_type client_credentials does that require username and password? 

Watsky
Solution Supplier
Solution Supplier

No, these are the only keys I have listed when I run it in Postman 

 

Watsky_0-1623781317937.png

Watsky_2-1623781592028.png

 


 


Did my answer(s) help you? Give it a kudos by clicking the Thumbs Up! ?
Did my post answer your question(s)? Mark my post as a solution. This will help others find the solution.

mnayar
Solution Specialist
Solution Specialist

Can you provide screenshot of the values you use for the above attributed?

Watsky
Solution Supplier
Solution Supplier

Sure at least everything but my client_id and secret. Also, I'm on GCC so my resource is a bit different, so no need copying that over.

apikey.png

 


Did my answer(s) help you? Give it a kudos by clicking the Thumbs Up! ?
Did my post answer your question(s)? Mark my post as a solution. This will help others find the solution.

mnayar
Solution Specialist
Solution Specialist

So I was able to get an authorization code, but when I use it in within Power BI, it expects me to sign in, that shouldnt be requirement right?

 

mnayar_1-1623783374644.png

mnayar_2-1623783404947.png

 

 

 

 

Watsky
Solution Supplier
Solution Supplier

It shouldn't be I just tested and I have my credentials set to Anoynmous.

 

Watsky_0-1623795067340.png

 


Did my answer(s) help you? Give it a kudos by clicking the Thumbs Up! ?
Did my post answer your question(s)? Mark my post as a solution. This will help others find the solution.

mnayar
Solution Specialist
Solution Specialist

Could you share your Power Query Code for getting token and using that token in the call to get workspaces just so that I can confirm I got everything in there? Also, can you confirm the settings in Azure I have made?

Watsky
Solution Supplier
Solution Supplier

Watsky_0-1623937375998.png

 as for your Azure settings. Has an admin granted privledges for the permissions? Looking at mine I have a granted/not granted by all of the permissions.

 

Watsky_1-1623937520528.png


Did my answer(s) help you? Give it a kudos by clicking the Thumbs Up! ?
Did my post answer your question(s)? Mark my post as a solution. This will help others find the solution.

 

mnayar
Solution Specialist
Solution Specialist

Everything above is the same. I get an unauthozed 401 message in Postman when I use the authorization code generated in Power BI. My guess is there is something different in Azure config. 

 

mnayar_0-1623952438645.png

 

Hi @mnayar.

I'd like to suggest you check if you used the same account on the power bi side which assigned the api permissions on the Azure side.

For this scenario, you can also contact azure support to trace the requests to get further information and support for troubleshooting.

BTW, it seems like you are work with admin API, have you assign the power bi admin permission on the test account?

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
PBI_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Get Ready for Power BI Dev Camp

Microsoft named a Leader in The Forrester Wave

Microsoft received the highest score of any vendor in both the strategy and current offering categories.

Get Ready for Power BI Dev Camp

Power BI Dev Camp - September 30th, 2021

Mark your calendars and join us for our next Power BI Dev Camp!

PowerPlatform 768x460.png

Microsoft Learn

Check out our new Discover Your Career Path blog post series and get all the details.