我们一直在努力追随这篇 Power BI 文章这样我们就可以将报告/仪表板嵌入到我们的 SaaS 产品中。具体来说,我们陷入了第 3 步“创建嵌入令牌”。
我们能够很好地获得不记名令牌,但是当检索报告的请求最终提交给我们收到的 API 时:操作返回无效状态代码“禁止”
private static string clientId = "...";
private static string secretKey = "...";
private static string groupId = "...";
static void Main(string[] args)
{
string resourceUri = "https://analysis.windows.net/powerbi/api";
string authorityUri = "https://login.windows.net/common/oauth2/authorize";
ClientCredential credential = new ClientCredential(clientId, secretKey);
AuthenticationContext authContext = new AuthenticationContext(authorityUri);
var token = authContext.AcquireTokenAsync(resourceUri, credential).Result.AccessToken;
var tokenCredentials = new TokenCredentials(token, "Bearer");
using (var client = new PowerBIClient(new Uri("https://api.powerbi.com/"), tokenCredentials))
{
var reports = client.Reports.GetReportsInGroupWithHttpMessagesAsync(groupId);
// !!! - Here's where the exception is thrown
// !!! -- Operation returned an invalid status code 'Forbidden'
var report = reports.Result.Body;
}
}
这是我们尝试过的:
- 所需的权限已被授予(我们已经检查了所有权限以确保我们没有遗漏任何内容)。这包括 Windows Azure Active Directory/Power BI 服务。
- 我们已确认客户端 ID、密钥和组 ID 正确。
- Power BI 工作空间是私有的,但我们已尝试创建一个公共工作空间以确保这不会产生影响。
- 最后,我们通过代码收到的令牌与 powerbi.com 上的令牌相匹配。
您正在使用客户端凭据流来获取 Power BI API 的令牌。目前,Power BI REST API 仅支持委派权限,但不支持任何应用程序权限。因此您的访问令牌无法获得足够的访问权限。要使用 Power BI,身份验证需要基于特定用户。相关主题here and here供您参考。
根据你的document,该场景是应用程序拥有对数据的访问权限。用户不一定是 Power BI 用户,应用程序控制最终用户的身份验证和访问。然后就可以使用资源所有者流程来获取token了。
其示例可在 Controllers\HomeController.cs 中找到应用程序拥有数据示例。
从代码示例中,它使用用户密码凭据(而不是应用程序的凭据)获取令牌:
// Create a user password cradentials.
var credential = new UserPasswordCredential(Username, Password);
// Authenticate using created credentials
var authenticationContext = new AuthenticationContext(AuthorityUrl);
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);
请参阅对用户进行身份验证并获取 Power BI 应用程序的 Azure AD 访问令牌并检查Access token for non-Power BI users (app owns data)
部分 。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)