Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Wahidm
Regular 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 initialize - Could not resolve cluster",
"errorCode": "403",
"level": 6,
"technicalDetails": {
"requestId": "5ccfb85d-9a69-49b4-b80f-1706f36bd590"
}
}

 

What am I doing?

This is my app settings:

"AppSettings": {
    "AuthorityUri": "https://login.windows.net/common/oauth2/authorize/",
    "ResourceUrl": "https://analysis.windows.net/powerbi/api",
    "RedirectUrl": "http://localhost:21638",
    "ApiUrl": "https://api.powerbi.com/",
    "ApplicationId": "MYAPPLICATIONID",
    "LoggingRequestUrl": "https://login.microsoftonline.com/common/oauth2/token",
    "GroupId": "MY WORKSPACE ID",
    "ReportID": "MY REPORTID",
    "ApplicationSecret": "MY APP SECRET",
    "Username": "POWERBI PRO USER ACCOUNT EMAIL",
    "Password": "MY PASSWORD"
   }

I get the Token using:

            var authorityUrl = $"https://login.microsoftonline.com/GROUPACCOUNTURL/oauth2/token";

            var oauthEndpoint = new Uri(authorityUrl);

            using (var client = new HttpClient())
            {
                var result = await client.PostAsync(oauthEndpoint, new FormUrlEncodedContent(new[]
                {
                     new KeyValuePair<string, string>("resource", AppSettings.ResourceUrl),
                     new KeyValuePair<string, string>("client_id", AppSettings.ApplicationId),
                     new KeyValuePair<string, string>("client_secret", AppSettings.ApplicationSecret),
                     new KeyValuePair<string, string>("grant_type", "password"),
                     new KeyValuePair<string, string>("username", AppSettings.Username),
                     new KeyValuePair<string, string>("password",AppSettings.Password),
                     new KeyValuePair<string, string>("scope", "openid"),
                 }));

                var content = await result.Content.ReadAsStringAsync();

                var authenticationResult = JsonConvert.DeserializeObject<OAuthResult>(content);
                return (new TokenCredentials(authenticationResult.AccessToken, "Bearer"), authenticationResult.AccessToken);

I then do the following:

 using (var powerBiClient = new PowerBIClient(new Uri(AppSettings.ApiUrl), credential.tokenCredentials))
            {
                var powerBiReport = powerBiClient.Reports.GetReportInGroup(AppSettings.GroupId, reportId);

                if (powerBiReport != null)
                {
                    var rowLevelSecurityIdentity = new List<EffectiveIdentity>
                                                       {
                                                           new EffectiveIdentity(name,
                                                               roles: new List<string> {role},
                                                               datasets: new List<string> {powerBiReport.DatasetId})
                                                       };
                    
                    var powerBiTokenRequestParameters = new GenerateTokenRequest("view", powerBiReport.DatasetId, identities: rowLevelSecurityIdentity);

                    var powerBiTokenResponse = powerBiClient.Reports.GenerateTokenInGroup(AppSettings.GroupId,powerBiReport.Id, powerBiTokenRequestParameters);

                    powerBIModel.EmbedUrl = powerBiReport.EmbedUrl;
                    powerBIModel.ReportId = powerBiReport.Id;
                    powerBIModel.AccessToken = powerBiTokenResponse.Token;

                }
  • I have set up the resource and the dedicated capacity in Azure
  • I have the diamond showing on the Workspace in PowerBi
  • I am the master user and I also have a PowerBi Pro account
  • It manages to get embed Url and access token but nothing shows up. I run the embed url, access token and report Id on the power bi playground site and get the error json above.

Can anyone help please?

 

Thanks

1 REPLY 1
MattStannett
Frequent Visitor

LoadReportFailedFail to initialize - Couldn't resolve cluster403* 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. 

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.