4
votes

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://docs.microsoft.com/en-us/power-bi/developer/register-app

Any help would be appreciated.

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
Could you check the headers of the response, is there one X-PowerBI-Error-Info and if yes, what is the value? - Andrey Nikolov
Did you ever figure this out? - lucuma

1 Answers

3
votes

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