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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
tripleacoder
Helper I
Helper I

Power Bi REST API - 401 Authorization error when using app secret

I have a console app that uses the REST API to get a dataset (and later add rows to it). This works when I supply my own user/password credentials.

 

Now I have registered the app as a Web/API app in order to use an app key/secret instead. I can get a token, but when I make the same REST call I get 401 Unauthorized.

 

I have given the app the following Application permissions in Power BI Service (is this needed?):

Read and write all content in tenant

View all content in tenant

These permissions have been granted by an Azure Administrator.

 

In addition to the Delegated permissions that worked with user authentication:

Read and write all Datasets

View all Datasets

 

I have decode the two tokens.

The token for app key autentication contains this:

"roles": [
"Tenant.ReadWrite.All",
"Tenant.Read.All"
],

 

while the token for user based authentication contains this:

"scp": "Dataset.ReadWrite.All Workspace.ReadWrite.All",

 

What am I missing..?

1 ACCEPTED SOLUTION

Hi @tripleacoder,

 

As far as I know, the permissions of Power BI are all based on the users. So an App can't act as a user. Please refer to developer/power-bi-permissions where all the descriptions have "user". 

Regarding "Tenant.ReadWrite.All", the documentation above also has a description. Actually, these permissions only can retrieve the profiles rather than data. Please refer to admin/reports_getreportsasadmin.

One simple proof we can see is that even an admin can't access all the App workspaces. The data is the precious asset of a company. I think this is reasonable.

 

 

Best Regards,
Dale

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

View solution in original post

22 REPLIES 22
pvuppala
Helper IV
Helper IV

Anyone got this worked out?  I'm running into the same issue where we need to assign workspaces to a capacity via this POST Call

Invoke-PowerBIRestMethod -Url 'admin/capacities/AssignWorkspaces' -Method Post -body $bodyStr

This works for my ID but fails for ServicePrincipal.  Is the ServicePrincipal only allowed for "Read-Only" API calls (GET method)?

 

Please let me know as we're trying to automate some scripts using ServicePrincipal/App ID.  Not sure what we're missing.

bingo
Microsoft Employee
Microsoft Employee

Hi, did you solve it, appreciate for your response, thanks!

Ended up using personal account to run the PowerShell script.  Eventually would like to use Power Automate via Service Principal client/secret.

ironmanwk
Regular Visitor

My teammate been trying to get REST API to work as well but also encounter the same 401 error message. Is this still an on-going issue or there is a solution we can apply?

Anonymous
Not applicable

Why does the sample code for the power bi embedded 'app owns data' scenario use the client credentials route when it is not supported? 

RashmiRaut
Frequent Visitor

Hi All,

 

I am also geeting the same error when accessing power BI rest API's. I am able to generate token but not proceeding further. Can you please paste your code, which ran successfully exclusive of all tennat id and username and pwd.

 

Thanks & Regards


@tripleacoder wrote:

I have a console app that uses the REST API to get a dataset (and later add rows to it). This works when I supply my own user/password credentials.

 

Now I have registered the app as a Web/API app in order to use an app key/secret instead. I can get a token, but when I make the same REST call I get 401 Unauthorized.

 

I have given the app the following Application permissions in Power BI Service (is this needed?):

Read and write all content in tenant

View all content in tenant

These permissions have been granted by an Azure Administrator.

 

In addition to the Delegated permissions that worked with user authentication:

Read and write all Datasets

View all Datasets

 

I have decode the two tokens.

The token for app key autentication contains this:

"roles": [
"Tenant.ReadWrite.All",
"Tenant.Read.All"
],

 

while the token for user based authentication contains this:

"scp": "Dataset.ReadWrite.All Workspace.ReadWrite.All",

 

What am I missing..?



@tripleacoder wrote:

I have a console app that uses the REST API to get a dataset (and later add rows to it). This works when I supply my own user/password credentials.

 

Now I have registered the app as a Web/API app in order to use an app key/secret instead. I can get a token, but when I make the same REST call I get 401 Unauthorized.

 

I have given the app the following Application permissions in Power BI Service (is this needed?):

Read and write all content in tenant

View all content in tenant

These permissions have been granted by an Azure Administrator.

 

In addition to the Delegated permissions that worked with user authentication:

Read and write all Datasets

View all Datasets

 

I have decode the two tokens.

The token for app key autentication contains this:

"roles": [
"Tenant.ReadWrite.All",
"Tenant.Read.All"
],

 

while the token for user based authentication contains this:

"scp": "Dataset.ReadWrite.All Workspace.ReadWrite.All",

 

What am I missing..?




 

v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi @tripleacoder,

 

Could you please mark the proper answers as solutions?

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi @tripleacoder,

 

It seems you only need an access token. Please refer to developer/embed-sample-for-customers

 

 

Best Regards,
Dale

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


@v-jiascu-msft wrote:

 

 

It seems you only need an access token. Please refer to developer/embed-sample-for-customers

 


That link talks about using a special user account (username + password) for the app, not an app secret/service principal, which is what I am trying to do.

 

However, I have been told elsewhere that roles are not needed in order to authorize service principals. Only "App permissions" are needed.

Hi @tripleacoder,

 

Your requirements are quite clear now. You'd like to use the App secret (aka client secret) instead of the user password authentication. I'm afraid this isn't workable in Power BI. The reason is simple. Even the global admin can't access other's contents. How can an App access everything?

 

Best Regards,
Dale

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


@v-jiascu-msft wrote:

 

Your requirements are quite clear now. You'd like to use the App secret (aka client secret) instead of the user password authentication. I'm afraid this isn't workable in Power BI. The reason is simple. Even the global admin can't access other's contents. How can an App access everything?



Thanks. This MS sample posts data to a custom Web API using the same method (but with a custom role):

https://azure.microsoft.com/en-us/resources/samples/active-directory-dotnet-daemon/

 

Can you explain a bit more why that won't work with the Power BI REST API? Perhaps also with a link to some documentation...

 

The app has the permission (=role) "Tenant.ReadWrite.All" which I supposed was for this purpose.

Hi @tripleacoder,

 

As far as I know, the permissions of Power BI are all based on the users. So an App can't act as a user. Please refer to developer/power-bi-permissions where all the descriptions have "user". 

Regarding "Tenant.ReadWrite.All", the documentation above also has a description. Actually, these permissions only can retrieve the profiles rather than data. Please refer to admin/reports_getreportsasadmin.

One simple proof we can see is that even an admin can't access all the App workspaces. The data is the precious asset of a company. I think this is reasonable.

 

 

Best Regards,
Dale

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

This is all fine except for one thing: You have to infer the "user Apps only" status of Power BI dataflows. 

 

Something explicit along these lines would have saved me the better part of a week chasing my tail. 

This doesn't make any sense.

 

Power BI released all of this documentation saying that consumers could use service principals to embed reports but we can't use service principals to list the reports in a workspace that the service principal is an admin member of?

tripleacoder
Helper I
Helper I

I found this link:

 

https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-porta...

It talks about assigning roles to the app.

 

I have now granted my app the Contributor role, but only on a Resource group.

It hasn't helped. I think the Poer BI service resides at the Subscription level... that means I will need help from a global admin again.

 

 

I had a simliar issue where i could use my own credentials but not the service account and the issue was the service account didnt have a power bi pro license. Not sure if thats the same issue but might be worth looking at


@sjc4062 wrote:

I had a simliar issue where i could use my own credentials but not the service account and the issue was the service account didnt have a power bi pro license. Not sure if thats the same issue but might be worth looking at


 

That might be it. But when I go to assign licenses and search for the service principal it does not come up in the results. Not sure if it is getting filtered away because only users and groups are valid, or if it's because I am not an Azure global admin.

zoloturu
Memorable Member
Memorable Member

@tripleacoder,

 

Could you try to mention an access level in API request?

 

https://docs.microsoft.com/en-us/rest/api/power-bi/embedtoken/reports_generatetoken#tokenaccesslevel

 

Regards,
Ruslan
-------------------------------------------------------------------
Did I answer your question? Mark my post as a solution!


 

Could you try to mention an access level in API request?

 

https://docs.microsoft.com/en-us/rest/api/power-bi/embedtoken/reports_generatetoken#tokenaccesslevel

 

 


I'm not sure what you mean. The link goes to "Required access level for EmbedToken generation", but I'm not using the Embed Token API.

 

I get the token using this code:

 

 

const string authorityUri = "https://login.microsoftonline.com/" + tenantId;

AuthenticationContext authContext = new AuthenticationContext(authorityUri);

        
AuthenticationResult result = null;
         
result = await authContext.AcquireTokenAsync(resourceUri, clientCredential);

 

Anonymous
Not applicable

@v-jiascu-msft  

 can any power bi pro user register an app for power bi in the below link. I have a user who is power bi admin, and he cant register an app. It throws some error related to access when registering thru this dev.powerbi.com/apps.

https://docs.microsoft.com/en-us/power-bi/developer/register-app

Can u help

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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

Top Kudoed Authors