wpf 操作返回了无效的状态代码“未授权”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49037233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Operation returned an invalid status code 'Unauthorized''
提问by Truecolor
I am trying to embed a sample Power BI dashboard in a WPF application following the sample project and sort of tutorial from here. When I launch the app, I have to enter my password to authenticate myself and when it tries to get the list of my Power BI workspaces with the getAppWorkspacesList()I am getting this error message
我正在尝试按照示例项目和来自此处的教程在 WPF 应用程序中嵌入示例 Power BI 仪表板。当我启动应用程序时,我必须输入我的密码来验证我自己,当它尝试获取我的 Power BI 工作区列表时,我getAppWorkspacesList()收到此错误消息
Microsoft.Rest.HttpOperationException: 'Operation returned an invalid status code 'Unauthorized''
Microsoft.Rest.HttpOperationException: '操作返回无效状态代码'未授权''
Can someone please help in pointing out why this error is occuring? I tried to look into the details of the error, but I am not understanding what could be causing the issue. I was able to embed a dashboard in a .Net Web App without an issue, so I don't think the problem is in my Power BI or Azure Directory account.
有人可以帮忙指出为什么会发生此错误吗?我试图查看错误的详细信息,但我不明白是什么导致了问题。我能够毫无问题地将仪表板嵌入到 .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));
}
}
回答by Bruce Chen
Based on your description, I assumed that you are using the Access token for Power BI users (user owns data) approach. I would recommend you use https://jwt.io/to decode the access_token after successfully invoked authContext.AcquireTokenAsync. Make sure the audis https://analysis.windows.net/powerbi/apiand check the permissions scope property scp.
根据您的描述,我假设您正在使用 Power BI 用户(用户拥有数据)方法的访问令牌。我建议您在成功调用后使用https://jwt.io/对 access_token 进行解码authContext.AcquireTokenAsync。确保aud是https://analysis.windows.net/powerbi/api并检查权限范围属性scp。
For Get Groups, the required scope would look as follows:
对于Get Groups,所需的范围如下所示:
Required scope: Group.Read.All or Group.ReadWrite.All or Workspace.Read.All or Workspace.ReadWrite.All
所需范围:Group.Read.All 或 Group.ReadWrite.All 或 Workspace.Read.All 或 Workspace.ReadWrite.All
You could also use fiddler or postman to simulate the request against the get groups endpoint with the access_token received in your WPF application to narrow down this issue.
您还可以使用 fiddler 或 postman 来模拟针对 get groups 端点的请求,并使用 WPF 应用程序中收到的 access_token 来缩小此问题的范围。
Moreover, you could follow Register an applicationto check your Azure AD app and make sure the required delegated permissions to Power BI Service (Microsoft.Azure.AnalysisServices)API have been correctly configured.
此外,您可以按照注册应用程序来检查您的 Azure AD 应用程序,并确保已正确配置对Power BI 服务 (Microsoft.Azure.AnalysisServices)API所需的委派权限。
回答by Xiaosu
We got the same error when we use app owns data approach. The way to solve that is described here.
当我们使用 app owns data 方法时,我们遇到了同样的错误。此处描述了解决该问题的方法。
Basically, the way to get access token documented in Microsoft websitedoes not work. We end up making a REST API call to https://login.microsoftonline.com/common/oauth2/tokenand post the following data:
基本上,获取 Microsoft网站中记录的访问令牌的方法不起作用。我们最终向https://login.microsoftonline.com/common/oauth2/token发出 REST API 调用并发布以下数据:
- grant_type: password
- scope: openid
- resource: https://analysis.windows.net/powerbi/api
- client_id: APPLICATION_ID
- client_secret: APPLICATION_SECRET
- username: USER_ID
- password: USER_PASSWORD
- grant_type:密码
- 范围:openid
- 资源:https: //analysis.windows.net/powerbi/api
- 客户端 ID:APPLICATION_ID
- 客户端秘密:APPLICATION_SECRET
- 用户名:USER_ID
- 密码:USER_PASSWORD
You will get a JSON back and then you can get the access_token which will be used when creating power bi client like this:
您将获得一个 JSON,然后您可以获得 access_token,它将在创建 power bi 客户端时使用,如下所示:
var mTokenCredentials = new TokenCredentials(accessToken, "Bearer");
using (var client = new PowerBIClient(new Uri("https://api.powerbi.com"), mTokenCredentials))
I hope this can help someone. Thisis the original post.
我希望这可以帮助某人。这是原帖。


