powerbi - 如何为 PowerBI 上的报告获取嵌入访问令牌?
问题描述
我一直在尝试生成嵌入访问令牌,以便可以将我在 Power BI 上制作的报告嵌入到 React.JS Web 应用程序中。我按照以下步骤操作https://community.powerbi.com/t5/Developer/How -To-Get-embed-token-using-Get-Post-only/td-p/294475。但我收到状态码 403(禁止)的错误。还有其他方法可以生成嵌入访问令牌吗?
解决方案
我已尝试使用 ASP.NET Core,以下是我使用的两个代码片段。根据注册应用程序的类型(本机或 Web),有两种不同的方法可以从 Azure Active Directory(AAD)上的注册应用程序获取访问令牌。不同之处在于 Web 应用程序中有一个客户端密码,我们可以使用 azure 门户创建它。您需要从 azure 门户中找到客户端 ID、AAD 租户 ID、应用程序密码才能使用以下代码。
本国的 :
[HttpGet]
[Route("GetAccessTokenNative")]
public async Task<string> GetAccessTokenNative()
{
string token = await GetTokenNative();
return token;
}
private static async Task<string> GetTokenNative()
{
var oauthEndpoint = new Uri("https://login.microsoftonline.com/<your directory ID>/oauth2/token");
using (var client = new HttpClient())
{
var result = await client.PostAsync(oauthEndpoint, new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("resource", "https://analysis.windows.net/powerbi/api"),
new KeyValuePair<string, string>("client_id", "client ID"),
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", "username"),//PoweBI username
new KeyValuePair<string, string>("password", "password"),//PowerBI password
new KeyValuePair<string, string>("scope", "openid"),
}));
var content = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<OAuthResult>(content).AccessToken;
}
}
OAuthResult.cs
public class OAuthResult
{
[JsonProperty("token_type")]
public string TokenType { get; set; }
[JsonProperty("scope")]
public string Scope { get; set; }
[JsonProperty("experies_in")]
public int ExpiresIn { get; set; }
[JsonProperty("ext_experies_in")]
public int ExtExpiresIn { get; set; }
[JsonProperty("experies_on")]
public int ExpiresOn { get; set; }
[JsonProperty("not_before")]
public int NotBefore { get; set; }
[JsonProperty("resource")]
public Uri Resource { get; set; }
[JsonProperty("access_token")]
public string AccessToken { get; set; }
[JsonProperty("refresh_token")]
public string RefreshToken { get; set; }
}
网页:
[HttpGet]
[Route("GetAccessTokenWeb")]
public async Task<string> GetAccessTokenWeb()
{
string token = await GetTokenWeb();
return token;
}
public static async Task<string> GetTokenWeb()
{
HttpClient client = new HttpClient();
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", "username"),//PowerBI username
new KeyValuePair<string, string>("password", "password"),//PowerBI password
new KeyValuePair<string, string>("client_id", "client ID"),
new KeyValuePair<string, string>("scope", "openid"),
new KeyValuePair<string, string>("client_secret", "client secret")
new KeyValuePair<string, string>("resource", "https://analysis.windows.net/powerbi/api")
});
HttpResponseMessage res = client.PostAsync("https://login.microsoftonline.com/<your directory ID>/oauth2/token", content).Result;
string json = await res.Content.ReadAsStringAsync();
AzureAdTokenResponseDto tokenRes = JsonConvert.DeserializeObject<AzureAdTokenResponseDto>(json);
return tokenRes.AccessToken;
}
AzureAdTokenResponseDto.cs
public class AzureAdTokenResponseDto
{
[JsonProperty("access_token")]
public string AccessToken { get; set; }
}
推荐阅读
- javascript - 如何让一个类从其他两个不同的类中获取实例?
- python - 基于类变量的列表操作
- c# - 捕获所有请求期望区域 .NET 5
- java - 如何在没有数据库的情况下测试自定义存储库层?测试通过,但方法没有得到覆盖
- excel - 如何根据另一列中的日期和时间添加一列中的值?
- git - Git stash 部分暂存文件
- typescript - 打字稿未在 Svelte html 部分中解析
- php - Azure AD Auth - 仅注销特定应用程序/?
- mysql - 错误:尝试使用正确凭据连接到远程数据库时出现 getaddrinfo ENOTFOUND
- flutter - Flutter Image assets 和 Stream 值仅在用户交互后呈现