繁体   English   中英

操作返回了无效的状态代码“未授权”

[英]Operation returned an invalid status code 'Unauthorized''

我正在尝试按照示例项目和来自此处的教程在 WPF 应用程序中嵌入示例 Power BI 仪表板。 当我启动应用程序时,我必须输入我的密码来验证自己的身份,当它尝试使用getAppWorkspacesList()获取我的 Power BI 工作区列表时,我收到此错误消息

Microsoft.Rest.HttpOperationException: '操作返回了无效的状态代码'未授权''

有人可以帮忙指出为什么会发生此错误吗? 我试图查看错误的详细信息,但我不明白是什么导致了问题。 我能够毫无问题地将仪表板嵌入到 .Net Web 应用程序中,因此我认为问题不在于我的 Power BI 或 Azure 目录帐户。

在此处输入图片说明

private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
Uri redirectUri = new Uri(ConfigurationManager.AppSettings["ida:RedirectUri"]);
private static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

private static string graphResourceId = ConfigurationManager.AppSettings["ida:ResourceId"];
private AuthenticationContext authContext = null;

TokenCredentials tokenCredentials = null;
string Token = null;
string ApiUrl = "https://api.powerbi.com";



public MainWindow()
{
    InitializeComponent();
    TokenCache TC = new TokenCache();
    authContext = new AuthenticationContext(authority, TC);
}


private void getAppWorkspacesList()
{
    using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
    {
        appWorkSpacesList.ItemsSource = client.Groups.GetGroups().Value.Select(g => new workSpaceList(g.Name, g.Id));                
    }
}

根据您的描述,我假设您正在使用 Power BI 用户(用户拥有数据)方法的访问令牌。 我建议您在成功调用authContext.AcquireTokenAsync后使用https://jwt.io/解码 access_token 。 确保audhttps://analysis.windows.net/powerbi/api并检查权限范围属性scp

对于Get Groups ,所需的范围如下所示:

所需范围:Group.Read.All 或 Group.ReadWrite.All 或 Workspace.Read.All 或 Workspace.ReadWrite.All

您还可以使用 fiddler 或 postman 使用 WPF 应用程序中收到的 access_token 模拟针对 get groups 端点的请求,以缩小此问题的范围。

此外,您可以按照注册应用程序来检查您的 Azure AD 应用程序,并确保已正确配置对Power BI 服务 (Microsoft.Azure.AnalysisServices) API 所需的委派权限。

当我们使用 app owns data 方法时,我们遇到了同样的错误。 此处描述解决该问题的方法。

基本上,获取 Microsoft 网站中记录的访问令牌的方法不起作用。 我们最终向https://login.microsoftonline.com/common/oauth2/token发出 REST API 调用并发布以下数据:

  • grant_type:密码
  • 范围:openid
  • 资源: https : //analysis.windows.net/powerbi/api
  • 客户端 ID:APPLICATION_ID
  • client_secret: APPLICATION_SECRET
  • 用户名:USER_ID
  • 密码:USER_PASSWORD

您将获得一个 JSON,然后您可以获得 access_token,它将在创建 power bi 客户端时使用,如下所示:

    var mTokenCredentials = new TokenCredentials(accessToken, "Bearer");
    using (var client = new PowerBIClient(new Uri("https://api.powerbi.com"), mTokenCredentials))

我希望这可以帮助某人。 是原帖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM