4

I have created my app and registered over https://dev.powerbi.com/Apps as Native. In Azure, I added myself ad global admin user, registered myself to the application as admin, granted all Power BI API permissions. I created a workspace, added myself an admin user. Uploaded a Power BI report in my workspace. Works well when I am on browser.

I am trying to connect my report by using ASP.NET 4.61 MVC. My credentials, username and password work, so no problem for this code below:

var credential = new UserPasswordCredential(Username, Password);

// Authenticate using created credentials
var authenticationContext = new AuthenticationContext(AuthorityUrl);
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ApplicationId, credential);

if (authenticationResult == null)
{
    result.ErrorMessage = "Authentication Failed.";
    return View(result);
}

var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");

However, I am receiving the error: Status: Unauthorized (401) on the line GetReportsInGroupAsync(Workspaceid); where workspaceId is matching with my workspace.

// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
   // Get a list of reports.
   var reports = await client.Reports.GetReportsInGroupAsync(WorkspaceId);
 ...
}

So the reports from my workspace I cannot reach because of unauthorization error, and I could not pass it. How can I authorize myself, I am already in AAD as Global Admin, added myself as application owner and in workspace I am already registered as admin.

I've found this topic below, but the answer did not fix my issue:unauthorized error 401 for power reports embedding

Followed this guideline, also did not work: https://learn.microsoft.com/en-us/power-bi/developer/register-app

Any help would be appreciated.

Andrey Nikolov
  • 12,967
  • 3
  • 20
  • 32
Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
  • 1
    What are the values that you're using for `AuthorityUrl` and `ResourceUrl`? Also make sure that in your registered application in Azure AD.. you go to Settings > required permissions > add Power BI Service > and select all the appropriate `Delegated Permissions` for PowerBI as you're getting the token as a user – Rohit Saigal Dec 17 '18 at 17:10
  • Could you check the headers of the response, is there one `X-PowerBI-Error-Info` and if yes, what is the value? – Andrey Nikolov Dec 17 '18 at 19:45
  • 1
    Did you ever figure this out? – lucuma Feb 15 '19 at 16:20

1 Answers1

5

I had exact the same issue, and I checked the API call's header X-PowerBI-Error-Info. What I found there was: ServicePrincipalIsNotAllowedByTenantAdminSwitch.

So, your's tenant's admin should switch on "Allow service principals to use with Power BI APIs" and apply it to security group where Service Principal resides:

Admin portal

zaggio
  • 51
  • 1
  • 2
  • I have the same symptoms of the OP, and I don't get an X-PowerBI-Error-Info header on the respose. Also, the switch is on for my tenant. – boylec1986 Mar 13 '20 at 03:01
  • I switched on `Allow service principals to use Power BI APIs` in the PBI Service Admin Portal. Went from getting the same Status 401: `X-PowerBI-Error-Info: ServicePrincipalIsNotAllowedByTenantAdminSwitch` response to a Status 403: `"Message": "API is not accessible for application"`. It said wait 15 mins for setting change to take effect. Maybe this will clear after the time has elapsed. – SeaDude Aug 10 '21 at 15:44
  • Is there anyway to automate this? e.g. terraform / powershell – Michael Blake Oct 27 '22 at 08:47