微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以使用“Client_credential”授权类型访问 powerBI 范围?

如何解决是否可以使用“Client_credential”授权类型访问 powerBI 范围?

在我的项目中,我试图在没有交互式登录的情况下从 powerBI 获取嵌入报告信息。 到目前为止,我能够使用“密码”授权获取信息;当我尝试使用“密码”授权来做同样的事情时,它给了我“未经授权”的错误。 我已在我的 link to github 中托管该应用程序。

使用说明:

  1. 获取访问令牌以获取用于身份验证的访问令牌。
var form = new Dictionary<string,string>();
form["grant_type"] = "password";
form["username"] = _config.Azure.Username;
form["password"] = _config.Azure.Password;
form["client_id"] = _config.Azure.ClientID;
form["client_secret"] = _config.Azure.ClientSecret;
form["scope"] = string.Join(" ",_config.Azure.Scopes);
var content = new FormUrlEncodedContent(form);
var input = new HttpInputModel
{
   BaseAddress = $"{_config.Azure.AuthorityUrl}/{_config.Azure.TenantID}/",Content = content,Headers = new Dictionary<string,string>
       {
           { "Content-Type","application/x-www-form-urlencoded" }
       },Url = "oauth2/v2.0/token"
};
  1. 使用上面的访问令牌来获取 powerBI 客户端。
var token = await _azureService.GetAccesstoken();
var tokenCredentials = new TokenCredentials(token,"Bearer");
_client = new PowerBIClient(new Uri(_config.PowerBI.ApiUrl),tokenCredentials);
  1. 使用客户端检索报告信息。
var workspaceId = reportInput.WorkspaceID;
var reportId = reportInput.ReportID;
var report = await _client.Reports.GetReportInGroupAsync(workspaceId,reportId);
var generatetokenRequestParameters = new GeneratetokenRequest(accessLevel: "view");
var tokenResponse = await _client.Reports.GeneratetokenAsync(workspaceId,reportId,generatetokenRequestParameters);
var output = new ReportOutput
{
     Username = _config.PowerBI.Username,EmbdedToken = tokenResponse,EmbedUrl = report.EmbedUrl,Id = reportInput.ReportID.ToString()
};
return output;

结论:在第 1 步:您可以看到我正在使用密码授权 类型。当我尝试用“client_crdentials”代替“password”时 不提供“用户名”和“密码”。生成的“访问令牌”是 在第 3 步中给出“未经授权”的例外。

注意:我尝试在很多论坛中搜索,其中一些论坛说我们可以使用“serviceprincipal”。但我做不到。

解决方法

我尝试关注微软的文章,在那篇文章中他们提到在 pwoerBI 中添加 SPN。我尝试了很多次,但它从来没有对我有用。 然后我使用了另一个帐户并且在那里工作,所以我的帐户问题不允许我添加 SPN。 我已经在 github eith 步骤中发布了整个解决方案。 链接在这里: https://github.com/Donjay2101/API--access-powerbi-without-login

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。