【问题标题】:is "Client_credential" grant type can be used to access powerBI scopes?“客户端凭据”授权类型是否可用于访问 Power BI 范围?
【发布时间】:2021-07-13 08:57:05
【问题描述】:

在我的项目中,我试图在没有交互式登录的情况下从 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. 使用上面的accesstoken来获取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”。但我做不到。

【问题讨论】:

  • 请在问题中添加相关代码,而不仅仅是指向外部网站的链接

标签: powerbi powerbi-embedded power-bi-report-server powerbi-api


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-11-27
    • 2020-02-10
    • 2015-04-16
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 2018-04-28
    相关资源
    最近更新 更多