【问题标题】:Operation returned an invalid status code 'Unauthorized''操作返回了无效的状态代码“未授权”
【发布时间】:2018-02-28 19:31:43
【问题描述】:

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

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

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

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));                
    }
}

【问题讨论】:

    标签: c# wpf azure-active-directory powerbi-embedded


    【解决方案1】:

    根据您的描述,我假设您正在使用 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 来模拟针对 get groups 端点的请求,并使用 WPF 应用程序中收到的 access_token 来缩小此问题的范围。

    此外,您可以关注 Register an application 检查您的 Azure AD 应用程序,并确保已正确配置 Power BI Service (Microsoft.Azure.AnalysisServices) API 所需的委派权限。 p>

    【讨论】:

    • 是的,首先,由于 OP 有一个graphResourceId,它表明资源 id 可能是错误的。
    • 相关代码示例为github.com/rreilly70/PowerBIEmbedded-Native-WPFBrowser/blob/…。代码示例中的配置是正确的,但是op仍然需要检查检索到的访问令牌。
    • @Bruc 谢谢。这是我的 Azure AD 中的权限问题。经过数小时努力编辑代码后,我决定授予 Power BI 服务 API 下所有类别的权限,并且成功了。
    • @BruceChen:跟踪您的答案,令牌中没有 scp 属性。是令牌生成的问题吗?
    【解决方案2】:

    当我们使用应用拥有数据的方法时,我们遇到了同样的错误。解决该问题的方法在here 中进行了描述。

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

    你会得到一个 JSON 回来,然后你可以得到 access_token 将在创建 Power bi 客户端时使用,如下所示:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      • 2020-04-24
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多