Я новичок в использовании API, но мне хотелось получить некоторую информацию о моем наборе данных.
Я создал приложение в активном каталоге Azure и добавил несколько разрешений API Power-Bi (read.all). Я не являюсь администратором, поэтому наш администратор предоставил согласие на все запрошенные мной API.
Я использую этот сценарий C#, чтобы получить токен доступа и зафиксировать время обновления моего отчета. Однако, несмотря на то, что я могу получить токен доступа, я все равно получаю несанкционированную ошибку.
Код: Выделить всё
using Microsoft.PowerBI.Api;
using Microsoft.PowerBI.Api.Models;
using Microsoft.Identity.Client;
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Rest;
using System.Collections.Generic;
class Program
{
static async Task Main(string[] args)
{
// Azure AD app credentials
string clientId = "";
string tenantId = "";
string workspaceIdString = "";
Guid workspaceId;
if (!Guid.TryParse(workspaceIdString, out workspaceId))
{
Console.WriteLine("Invalid workspace ID format.");
return;
}
Console.WriteLine("1");
string datasetId = "";
var scopes = new[] { "https://analysis.windows.net/powerbi/api/Dataset.Read.All" };
string clientSecret = "";
Console.WriteLine("2");
// access token
var accessToken = await GetAccessTokenAsync(clientId, clientSecret,tenantId);
Console.WriteLine($"{accessToken}");
// Create a Power BI client
var client = new PowerBIClient(new Uri("https://api.powerbi.com"), new TokenCredentials(accessToken, "Bearer"));
// Get refresh history
var refreshes = await client.Datasets.GetRefreshHistoryInGroupAsync(workspaceId, datasetId);
// Display refresh history
foreach (var refresh in refreshes.Value)
{
Console.WriteLine($"Refresh ID: {refresh.RequestId}");
Console.WriteLine($"Start Time: {refresh.StartTime}");
Console.WriteLine($"End Time: {refresh.EndTime}");
Console.WriteLine($"Status: {refresh.Status}");
Console.WriteLine();
}
}
static async Task GetAccessTokenAsync(string clientId, string clientSecret, string tenantId)
{
var app = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(AzureCloudInstance.AzurePublic, tenantId)
.Build();
var scopes = new[] { "https://analysis.windows.net/powerbi/api/.default" };
var accounts = await app.GetAccountsAsync();
var firstAccount = accounts.FirstOrDefault();
AuthenticationResult authResult;
try
{
if (firstAccount != null)
{
authResult = await app.AcquireTokenSilent(scopes, firstAccount).ExecuteAsync();
}
else
{
authResult = await app.AcquireTokenForClient(scopes).ExecuteAsync();
}
}
catch (MsalUiRequiredException)
{
// Handle the exception appropriately for your scenario
throw;
}
return authResult.AccessToken;
}
}
Is there any setting that i have change? or does it have to do with me being non admin? Honestly not sure what's going on.
I looked around, and used the manual signing in process, and the script worked fine. But now that i added access token part, i am getting "unauthorized" error.
I should mention that i am getting the access token, so there is no problem with my client id, secret and tenant id.
Источник: https://stackoverflow.com/questions/764 ... on-have-al