Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
I have a Native App. I am trying to Embed (App Owns Data) power bi content to my Native App. I am trying to embed a report/ dashboard. To do so I have to generate the Embed Token. When I try to generate the embed token it gives me 500 internal Server error or Forbidden status. Please see the attached error information. What could the reason for this error ? Is this because of Access issue?
Below is my code:
public async Task<ActionResult> GetReportByID(String Id)
{
var error = GetWebConfigErrors();
if (error != null)
{
return View(new EmbedConfig()
{
ErrorMessage = error
});
}
// 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);
if (authenticationResult == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Authentication Failed."
});
}
var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
// Get a list of reports.
var reports = await client.Reports.GetReportsInGroupAsync(GroupId);
var report = reports.Value.FirstOrDefault();
// Get the first report in the group.
foreach (var item in reports.Value)
{
if (Id == item.Id)
{
report = item;
}
}
if (report == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Group has no reports."
});
}
// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, Id, generateTokenRequestParameters);
if (tokenResponse == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Failed to generate embed token."
});
}
// Generate Embed Configuration.
var embedConfig = new EmbedConfig()
{
EmbedToken = tokenResponse,
EmbedUrl = report.EmbedUrl,
Id = report.Id
};
return View(embedConfig);
}
}
Solved! Go to Solution.
@n02378281 wrote:
I have a Native App. I am trying to Embed (App Owns Data) power bi content to my Native App. I am trying to embed a report/ dashboard. To do so I have to generate the Embed Token. When I try to generate the embed token it gives me 500 internal Server error or Forbidden status. Please see the attached error information. What could the reason for this error ? Is this because of Access issue?
Below is my code:
public async Task<ActionResult> GetReportByID(String Id)
{var error = GetWebConfigErrors();
if (error != null)
{
return View(new EmbedConfig()
{
ErrorMessage = error
});
}// 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);if (authenticationResult == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Authentication Failed."
});
}var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
// Get a list of reports.
var reports = await client.Reports.GetReportsInGroupAsync(GroupId);var report = reports.Value.FirstOrDefault();
// Get the first report in the group.
foreach (var item in reports.Value)
{
if (Id == item.Id)
{
report = item;
}
}if (report == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Group has no reports."
});
}
// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, Id, generateTokenRequestParameters);if (tokenResponse == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Failed to generate embed token."
});
}// Generate Embed Configuration.
var embedConfig = new EmbedConfig()
{
EmbedToken = tokenResponse,
EmbedUrl = report.EmbedUrl,
Id = report.Id
};return View(embedConfig);
}}
The above code works in my test. Usually Forbidden error indicates the registered app doesn't have suffiecient permission, please check premissions in azure portal. For other issues, to have a better troubleshooting, please add try..catch block to get more detailed error information.
try { // Generate Embed Token. var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view"); var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, report.Id, generateTokenRequestParameters); if (tokenResponse == null) { return View(new EmbedConfig() { ErrorMessage = "Failed to generate embed token." }); } // Generate Embed Configuration. var embedConfig = new EmbedConfig() { EmbedToken = tokenResponse, EmbedUrl = report.EmbedUrl, Id = report.Id }; return View(embedConfig); } catch (HttpOperationException ex) { //debug content for error details
var content = ex.Response.Content; }
By the way, when playing with Power BI API lib, I'd like to test the correlated rest api(GenerateToken in this case) with Postman at first.
@n02378281 wrote:
I have a Native App. I am trying to Embed (App Owns Data) power bi content to my Native App. I am trying to embed a report/ dashboard. To do so I have to generate the Embed Token. When I try to generate the embed token it gives me 500 internal Server error or Forbidden status. Please see the attached error information. What could the reason for this error ? Is this because of Access issue?
Below is my code:
public async Task<ActionResult> GetReportByID(String Id)
{var error = GetWebConfigErrors();
if (error != null)
{
return View(new EmbedConfig()
{
ErrorMessage = error
});
}// 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);if (authenticationResult == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Authentication Failed."
});
}var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
// Get a list of reports.
var reports = await client.Reports.GetReportsInGroupAsync(GroupId);var report = reports.Value.FirstOrDefault();
// Get the first report in the group.
foreach (var item in reports.Value)
{
if (Id == item.Id)
{
report = item;
}
}if (report == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Group has no reports."
});
}
// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, Id, generateTokenRequestParameters);if (tokenResponse == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Failed to generate embed token."
});
}// Generate Embed Configuration.
var embedConfig = new EmbedConfig()
{
EmbedToken = tokenResponse,
EmbedUrl = report.EmbedUrl,
Id = report.Id
};return View(embedConfig);
}}
The above code works in my test. Usually Forbidden error indicates the registered app doesn't have suffiecient permission, please check premissions in azure portal. For other issues, to have a better troubleshooting, please add try..catch block to get more detailed error information.
try { // Generate Embed Token. var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view"); var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, report.Id, generateTokenRequestParameters); if (tokenResponse == null) { return View(new EmbedConfig() { ErrorMessage = "Failed to generate embed token." }); } // Generate Embed Configuration. var embedConfig = new EmbedConfig() { EmbedToken = tokenResponse, EmbedUrl = report.EmbedUrl, Id = report.Id }; return View(embedConfig); } catch (HttpOperationException ex) { //debug content for error details
var content = ex.Response.Content; }
By the way, when playing with Power BI API lib, I'd like to test the correlated rest api(GenerateToken in this case) with Postman at first.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
14 | |
2 | |
1 | |
1 | |
1 |
User | Count |
---|---|
21 | |
2 | |
2 | |
2 | |
2 |