Forum Discussion
Wahidm
7 years agoRegular Visitor
Embed for customers- Fail to initialize - Could not resolve cluster
Hi, I am trying to use PowerBi Embeded in our application for our customers, however I get the following: > Json Object { "message": "LoadReportFailed", "detailedMessage": "Fail to initia...
MattStannett
7 years agoFrequent Visitor
| LoadReportFailed | Fail to initialize - Couldn't resolve cluster | 403 | * Bad access token * Embed type doesn't match token type |
As per https://docs.microsoft.com/en-us/power-bi/developer/embedded-troubleshoot.
My code is pretty similar and it works:
public async Task<AzureToken> GetAzureTokenDataAsync()
{
string authorityUrl = $"{_azureSettings.Instance}{_azureSettings.TenantId}/oauth2/token";
Uri oauthEndpoint = new Uri(authorityUrl);
using (HttpClient httpClient = new HttpClient())
{
HttpResponseMessage responseMessage = await httpClient.PostAsync(oauthEndpoint, new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("resource", _powerBiSettings.ResourceUrl),
new KeyValuePair<string, string>("client_id", _azureSettings.ClientId),
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", _powerBiSettings.Username),
new KeyValuePair<string, string>("password", _powerBiSettings.Password),
new KeyValuePair<string, string>("scope", "openid"),
}));
string responseText = await responseMessage.Content.ReadAsStringAsync();
OAuthResult authenticationResult = JsonConvert.DeserializeObject<OAuthResult>(responseText);
TokenCredentials tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
return new AzureToken(tokenCredentials, authenticationResult.AccessToken);
}
}
private class OAuthResult
{
[JsonProperty("token_type")]
public string TokenType { get; set; }
[JsonProperty("scope")]
public string Scope { get; set; }
[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }
[JsonProperty("ext_expires_in")]
public int ExtExpiresIn { get; set; }
[JsonProperty("expires_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; }
}
public async Task<EmbeddedReportConfig> GetReportForIdAsync(Guid reportId, string name, string roles)
{
var result = new EmbeddedReportConfig();
try
{
AzureToken azureToken = await _authenticationHandler.GetAzureTokenDataAsync();
using (PowerBIClient powerBiClient = new PowerBIClient(new Uri(_powerBiSettings.ApiUrl), azureToken.TokenCredentials))
{
Report powerBiReport = await powerBiClient.Reports.GetReportAsync(_powerBiSettings.WorkspaceId, reportId.ToString());
List<EffectiveIdentity> rowLevelSecurityIdentity = new List<EffectiveIdentity>
{
new EffectiveIdentity(
name,
//roles: new List<string> { role },
datasets: new List<string> {powerBiReport.DatasetId}
)
};
GenerateTokenRequest powerBiTokenRequestParameters = new GenerateTokenRequest("View", powerBiReport.DatasetId, false, rowLevelSecurityIdentity);
EmbedToken powerBiTokenResponse = await powerBiClient.Reports.GenerateTokenInGroupAsync(_powerBiSettings.WorkspaceId, powerBiReport.Id, powerBiTokenRequestParameters);
return new EmbeddedReportConfig
{
Id = powerBiReport.Id,
Name = powerBiReport.Name,
EmbedUrl = powerBiReport.EmbedUrl,
AccessToken = powerBiTokenResponse.Token
};
}
}
catch (HttpOperationException ex)
{
// https://community.powerbi.com/t5/Developer/quot-shouldn-t-have-effective-identity-quot-error-when-passing/td-p/433730
// https://docs.microsoft.com/en-us/power-bi/developer/embedded-row-level-security
//Bad Request
var content = ex.Response.Content;
Console.WriteLine(content);
return null;
}
}There is also this https://stackoverflow.com/a/52998483/5209435. I hope this helps.