我们一直在尝试阅读这篇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;
}
}
这是我们尝试过的方法:
您正在使用客户端凭证流来获取Power BI API的令牌。当前,Power BI REST API仅支持委派权限,但不支持任何应用程序权限。因此,您的访问令牌没有足够的访问权限。要使用Power BI,身份验证需要基于特定用户。这里和这里的相关主题供您参考。
根据您的文档,该场景是应用拥有对数据的访问权。用户不一定是Power BI用户,并且该应用程序控制最终用户的身份验证和访问。然后,您可以使用资源所有者流来获取令牌。
“应用拥有数据”示例的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)
部分。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句