【发布时间】:2018-08-09 19:09:40
【问题描述】:
我在使用powerbi api调用授权任务时遇到了一些问题。它在Authorize().Wait();抛出AggregateException异常。我也用谷歌搜索了它,但找不到任何解决方案。任何帮助,将不胜感激。这是我的Page_Load 函数代码
protected void Page_Load(object sender, EventArgs e)
{
credential = new UserCredential(Username, Password);
Authorize().Wait();
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
EmbedToken embedToken = client.Reports.GenerateTokenInGroup(<groupId>, <reportId>, new GenerateTokenRequest(accessLevel: "View", datasetId:<datasetId>));
Report report = client.Reports.GetReportInGroup(<groupId>,<reportId> );
System.Diagnostics.Debug.WriteLine("This is embed token");
System.Diagnostics.Debug.WriteLine(embedToken);
System.Diagnostics.Debug.WriteLine("this is embed url");
System.Diagnostics.Debug.WriteLine(report.EmbedUrl);
}
}
在这个函数中,我提取嵌入令牌并嵌入 url 并在输出窗口中打印,下面是授权函数代码
private static Task Authorize()
{
return Task.Run(async () =>
{
authenticationResult = null;
tokenCredentials = null;
var authenticationContext = new AuthenticationContext("https://login.windows.net/common/oauth2/authorize/");
authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, clientId, credential);
if (authenticationResult != null)
{
tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
}
});
}
【问题讨论】:
-
请检查
AggregateException的内部异常并确保发布所有信息。为什么在这种情况下使用Task.Run?因为AcquireTokenAsync已经返回Task,所以不需要它。此外,您应该删除阻塞调用并将签名更改为 protected async void Page_Load(object sender, EventArgs e)
标签: c# azure async-await powerbi