Forbidden error Power BI API GenerateTokenInGroup & GenerateTokenForCreateInGroup

huangapple go评论76阅读模式
英文:

Forbidden error Power BI API GenerateTokenInGroup & GenerateTokenForCreateInGroup

问题

最近我遇到了GenerateTokenInGroup和GenerateTokenForCreateInGroup API的问题,并收到了"操作返回了无效的状态代码 'Forbidden'"错误消息。上周它还能正常工作。然而,使用相同的设置,我可以成功执行GetReportsInGroup API。

public class Secrets
{
    private static string authorityUrl = "https://login.microsoftonline.com/organizations/";
    private static string resourceUrl = "https://analysis.windows.net/powerbi/api";
    public static string apiUrl = "https://api.powerbi.com/";
    private static string ClientID = ""; // Azure AD应用程序 - 应用程序ID/客户端ID
    private static string ClientSecret = ""; // Azure AD应用程序 - 客户端密钥
    private static string TenantId = ""; // Azure AD租户ID
    public static Guid groupId = Guid.Parse(""); // Power BI工作区ID
    private static ClientCredential credential = null;
    private static AuthenticationResult authenticationResult = null;
    public static TokenCredentials tokenCredentials = null;
    public static Task Authorize()
    {
        return Task.Run(async () =>
        {
            credential = new ClientCredential(ClientID, ClientSecret);
            authenticationResult = null;
            tokenCredentials = null;
            var tenantSpecificURL = authorityUrl.Replace("organizations", Secrets.TenantId);
            var authenticationContext = new AuthenticationContext(tenantSpecificURL);
            authenticationResult = await authenticationContext.AcquireTokenAsync(resourceUrl, credential);
            if (authenticationResult != null)
            {
                tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
            }
        });
    }
}
public class PowerBIController : ApiController
{
    private PowerBIClient PowerBIClient { get; set; }
    public PowerBIController()
    {
        Secrets.Authorize().Wait();
        PowerBIClient = new(new Uri(Secrets.apiUrl), Secrets.tokenCredentials);
    }
    [HttpGet]
    public object GetReportsInGroup()
    {
        return PowerBIClient.Reports.GetReportsInGroup(Secrets.groupId).Value;
    }
    [HttpGet]
    public object GenerateTokenInGroup(Guid reportId, Guid datasetId)
    {
        return PowerBIClient.Reports.GenerateTokenInGroup(Secrets.groupId, reportId, new GenerateTokenRequest(accessLevel: "View", datasetId: datasetId.ToString()));
    }
    [HttpGet]
    public object GenerateTokenForCreateInGroup(Guid datasetId)
    {
        return PowerBIClient.Reports.GenerateTokenForCreateInGroup(Secrets.groupId, new GenerateTokenRequest(accessLevel: "Create", datasetId: datasetId.ToString(), allowSaveAs: true));
    }
}

我已分配了这些API权限。

英文:

I am recently having an issue with GenerateTokenInGroup & GenerateTokenForCreateInGroup API and getting the "Operation returned an invalid status code 'Forbidden'" error. It was working fine last week. However, with the same settings, I can execute GetReportsInGroup API successfully.

public class Secrets
{
    private static string authorityUrl = "https://login.microsoftonline.com/organizations/";
    private static string resourceUrl = "https://analysis.windows.net/powerbi/api";
    public static string apiUrl = "https://api.powerbi.com/";
    private static string ClientID = ""; // The Azure AD App - Application ID/Client ID
    private static string ClientSecret = ""; // The Azure AD App - Client Secret
    private static string TenantId = ""; // The Azure AD Tenant Id
    public static Guid groupId = Guid.Parse("");//Power BI Workspace ID
    private static ClientCredential credential = null;
    private static AuthenticationResult authenticationResult = null;
    public static TokenCredentials tokenCredentials = null;
    public static Task Authorize()
    {
        return Task.Run(async () =>
        {
            credential = new ClientCredential(ClientID, ClientSecret);
            authenticationResult = null;
            tokenCredentials = null;
            var tenantSpecificURL = authorityUrl.Replace("organizations", Secrets.TenantId);
            var authenticationContext = new AuthenticationContext(tenantSpecificURL);
            authenticationResult = await authenticationContext.AcquireTokenAsync(resourceUrl, credential);
            if (authenticationResult != null)
            {
                tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
            }
        });
    }
}
public class PowerBIController : ApiController
{
    private PowerBIClient PowerBIClient { get; set; }
    public PowerBIController()
    {
        Secrets.Authorize().Wait();
        PowerBIClient = new(new Uri(Secrets.apiUrl), Secrets.tokenCredentials);
    }
    [HttpGet]
    public object GetReportsInGroup()
    {
        return PowerBIClient.Reports.GetReportsInGroup(Secrets.groupId).Value;
    }
    [HttpGet]
    public object GenerateTokenInGroup(Guid reportId, Guid datasetId)
    {
        return PowerBIClient.Reports.GenerateTokenInGroup(Secrets.groupId, reportId, new GenerateTokenRequest(accessLevel: "View", datasetId: datasetId.ToString()));
    }
    [HttpGet]
    public object GenerateTokenForCreateInGroup(Guid datasetId)
    {
        return PowerBIClient.Reports.GenerateTokenForCreateInGroup(Secrets.groupId, new GenerateTokenRequest(accessLevel: "Create", datasetId: datasetId.ToString(), allowSaveAs: true));
    }
}

I have assigned these API Permissions

答案1

得分: 0

我已经自己找到了解决方法。实际上,我正在使用Power BI嵌入试用版,现在已经完全消耗完了。可以使用GetAvailableFeatures API来检查。但是,与特定容量错误不同,生成令牌的API只会响应一个简单的禁止错误。

英文:

I have figured it out myself. Actually, I was using the Power BI Embed trail and now it is completely consumed. It can be checked by using GetAvailableFeatures API.
But instead of a specific capacity error, token-generating APIs are responding with just a simple Forbidden error.

huangapple
  • 本文由 发表于 2023年8月10日 17:10:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定