我正在尝试使用this link.
提供的示例将强力报告嵌入网页在此示例中,我已替换了我的专业帐户的所有客户ID和用户名密码。
我在App Registration中设置了Native应用程序类型。当我试图获得Embed Token时,会出现以下错误:
这是我的代码:
public async Task<ActionResult> EmbedReport()
{
// Create a user password credentials.
UserPasswordCredential UserCredentials = new UserPasswordCredential(Username, Password);
AuthenticationContext AuthContext = new AuthenticationContext(AuthorityUrl, false);
// Authenticate using created credentials
//AuthenticationResult AuthResult = await AuthContext.AcquireTokenAsync(ResourceUrl, clientCred);
AuthenticationResult AuthResult = await AuthContext.AcquireTokenAsync(ResourceUrl, ClientId, UserCredentials);
if (AuthResult == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Authentication Failed."
});
}
var tokenCredentials = new TokenCredentials(AuthResult.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 = client.Reports.GetReports();
// Get the first report in the group.
var report = reports.Value.FirstOrDefault();
if (report == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Group has no reports."
});
}
string accessLevel = "View";
// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel, report.DatasetId,false);
var tokenResponse = client.Reports.GenerateToken(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);
}
}