【发布时间】:2021-07-13 08:57:05
【问题描述】:
在我的项目中,我试图在没有交互式登录的情况下从 powerBI 获取嵌入报告信息。 到目前为止,我能够使用“密码”授权获取信息;当我尝试使用“密码”授权做同样的事情时,它给了我“未经授权”的错误。 我已经在我的link to github 中托管了该应用程序。
使用说明:
- 获取访问令牌以获取访问令牌进行身份验证。
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"
};
- 使用上面的accesstoken来获取powerBI客户端。
var token = await _azureService.GetAccessToken();
var tokenCredentials = new TokenCredentials(token, "Bearer");
_client = new PowerBIClient(new Uri(_config.PowerBI.ApiUrl), tokenCredentials);
- 使用客户端检索报告信息。
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