Forum Discussion
Operation returned an Invalid status code 'Unauthorized' (User owns data)
All,
I'm stuck in calling the PowerBi API from my project. What I did was create a MVC application, login with AzureId for the whole site, get a token for PowerBi and get a report. All goes well, except the retrievel of the report. No matter what call I do to the PowerBi Client, if only gives me Unauthorized.
When I try the same for https://docs.microsoft.com/nl-nl/rest/api/power-bi/reports/getreports#code-try-0 It all goes well, so the user who logges in is not the problem.
When I go back to my app and look at the token (via https://jwt.io/), I also think I have the correct permissions, which are set in the Azure portal:
"scp": "App.Read.All Capacity.Read.All Capacity.ReadWrite.All Content.Create Dashboard.Read.All Dashboard.ReadWrite.All Data.Alter_Any Dataflow.Read.All Dataflow.ReadWrite.All Dataset.Read.All Dataset.ReadWrite.All Gateway.Read.All Gateway.ReadWrite.All Group.Read Group.Read.All Metadata.View_Any Report.Read.All Report.ReadWrite.All StorageAccount.Read.All StorageAccount.ReadWrite.All Tenant.Read.All Tenant.ReadWrite.All Workspace.Read.All Workspace.ReadWrite.All",
No matter what call I make to the Api, all calls are Unauthorized (the calls below are all I tried)
var tokenCredentials = new TokenCredentials(Request.Cookies["token"].ToString(), "Bearer");
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
var groupid = new Guid("guid of the group");
var a = client.Reports.GetReportsInGroup(groupid);
var x = client.Groups.GetGroups();
var r1 = client.Reports.GetReports();
}
Does anyone have a clue what I'm doing wrong?
In addition the following lines generate an authorization code and access_code when not yet available:
public void GetAuthorizationCode()
{
var @params = new NameValueCollection
{ {"response_type", "code"},
{ "client_id", ClientID},
{ "resource", "https://analysis.windows.net/powerbi/api"},
{ "redirect_uri", RedirectURL} };
var queryString = HttpUtility.ParseQueryString(string.Empty);
queryString.Add(@params);
string strUrl = $"https://login.windows.net/common/oauth2/authorize?{queryString}";
Response.Redirect(strUrl);
}
public async Task<string> GetAccessToken(string authorizationCode, string clientID, string clientSecret, string redirectUri)
{
TokenCache TC = new TokenCache();
string authority = "https://login.microsoftonline.com/common";
AuthenticationContext AC = new AuthenticationContext(authority, TC);
ClientCredential cc = new ClientCredential(clientID, clientSecret);
AuthenticationResult result = await AC.AcquireTokenByAuthorizationCodeAsync(authorizationCode, new Uri(redirectUri), cc);
return result.AccessToken;
}
2 Replies
- ivilasboasEnerRegular Visitor
Hi Jaspertje
Did you manage to solve this?
I'm having the same problem.
Best Regards- JaspertjeFrequent Visitor
Yes we have, but I'm not sure what the correct solution was anymore. I Believe it had to do with the application not having access to the datasource (which seems obvious)
We added a mapping for the Application created in Azure to a hardcoded user.
To do so, go to the "Manage Gateway" screen. Select the gateway you use in the report you want to show. Select the Users and click the "Map user names" button below.
Change the rule for this datasource to "CustomData" and add the ObjectId! as Replace and a existing user with right to the datasource as With.
The ObjectId is not the one you can see in the overview via App registrations, but the one shown when you click the link behind "Managed Application in local directory" (The objectId for the Enterprise Application)
If you created an Enterprise application you should be able to use that one I suppose