I want automatic login to view power bi reports with iframe in mvc application.
For this, I created an application in Azure AD and gave all read permissions in the delegated permissions.
For this i created a class and get access token. But when i call service with token i get unauthorized error.
The class I created is as follows;
public async Task<string> GetAccessTokenAsync()
{
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(new Uri($"https://login.microsoftonline.com/{tenantId}"))
.Build();
string[] scopes = new string[] { "https://analysis.windows.net/powerbi/api/.default" };
AuthenticationResult result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
Console.WriteLine("token: " + result.AccessToken);
return result.AccessToken;
}
public async Task<string> GetReportsAsync()
{
string accessToken = await GetAccessTokenAsync();
using HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = await client.GetAsync("https://api.powerbi.com/v1.0/myorg/reports");
return await response.Content.ReadAsStringAsync();
}
How can I solve this problem?