Reputation: 41
I have been trying to generate embed access token so that I can embed a report I have made on Power BI in a React.JS web app.I followed the following steps https://community.powerbi.com/t5/Developer/How-To-Get-embed-token-using-Get-Post-only/td-p/294475 . But I am getting an error with status code 403(Forbidden). Is there some other way to generate an embed access token?
Upvotes: 4
Views: 2812
Reputation: 3876
I have tried with ASP.NET Core and the following are the two code snippets which I have used. According to the types of the registered application(Native or Web) there are two seperate ways to get access token from registered application on Azure Active Directory(AAD). The difference is there is a client secret in web application that we can create using azure portal. You need to find Client ID, AAD tenant ID, App secret from azure portal to use following code.
Native :
[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; }
}
Web :
[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; }
}
Upvotes: 1