我正在尝试为PowerBI报告执行数据集刷新。我创建了网关,我可以从管理门户进行数据集刷新。我可以通过管理门户中的UI(即上次刷新列)验证刷新是否成功。但是当我尝试从C#webapi代码进行刷新时,我得到了下面提到的错误。
Error Message:
The remote server returned an error: (401) Unauthorized.
Stack Trace:
at System.Net.HttpWebRequest.GetResponse()
at BlueSkyPowerBIService.Controllers.PowerBIController.<RefreshDatasetsForReports>d__13.MoveNext() in C:\Krishnan\RSI\SourceCode\Bluesky Developement\BlueSky Development\BlueSkyPowerBIService\BlueSkyPowerBIService\Controllers\PowerBIController.cs:line 258
在刷新代码之前,我能够再次对Azure AD进行身份验证并成功生成auth令牌,但是当它调用API进行刷新时,它会因上述错误而崩溃。
请找到我用于数据刷新的代码
List<ReportDetails> reportDetailsList = new List<ReportDetails>();
var result = new EmbedConfig();
ReportDetails reportDetails = new ReportDetails();
try
{
result = new EmbedConfig { Username = username, Roles = roles };
var error = GetWebConfigErrors();
if (error != null)
{
result.ErrorMessage = error;
//return View(result);
return null;
}
var credential = new UserPasswordCredential(Username, Password);
var authenticationContext = new AuthenticationContext(AuthorityUrl);
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);
if (authenticationResult == null)
{
result.ErrorMessage = "Authentication Failed.";
//return View(result);
return null;
}
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.GetReportsAsync();
for (int index = 0; index < reports.Value.Count; index++)
{
reportDetails = new ReportDetails();
Report report = reports.Value[index];
HttpWebRequest request;
if (report.Id == "6317f207-57d3-4f5f-9644-18bfbb9bef99")
{
var url = "https://api.powerbi.com/v1.0/myorg/groups/{0}/datasets/{1}/refreshes";
request = System.Net.HttpWebRequest.CreateHttp(String.Format(url, GroupId, report.DatasetId));
request.KeepAlive = true;
request.Method = "POST";
request.ContentLength = 0;
request.Headers.Add("Authorization", String.Format("Bearer {0}", authenticationResult.AccessToken));
using (Stream writer = request.GetRequestStream())
{
var response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Dataset refresh request{0}", response.StatusCode.ToString());
}
}
}//end for(int index=0; index< reports.Value.Count; index++)
return reportDetailsList;
}
}
catch (HttpOperationException exc)
{
result.ErrorMessage = string.Format("Status: {0} ({1})\r\nResponse: {2}\r\nRequestId: {3}", exc.Response.StatusCode, (int)exc.Response.StatusCode, exc.Response.Content, exc.Response.Headers["RequestId"].FirstOrDefault());
}
catch (Exception exc)
{
result.ErrorMessage = exc.ToString();
}
下的Azure门户中授予了所有必需的权限
为什么我收到此错误的任何想法?如何解决这个问题?
答案 0 :(得分:0)
这看起来像是用于执行刷新的Azure Active Directory应用程序的权限的错误配置。如图here所示,我们需要注册本机应用程序并声明一些权限才能访问Power BI rest API。
您需要确保此应用具有Dataset.ReadWrite.All
权限。您可以在Azure Active Directory -> App Registrations
下的Azure门户中查看甚至更改应用程序的权限。如果您无法看到该应用程序,请从右侧的下拉菜单中选择All apps
。
这就是我们的应用程序看起来如何成功使用通过其余API执行刷新