用于Power Bi管理的Azure AD代理流

时间:2019-01-21 12:00:18

标签: c# azure azure-active-directory powerbi msal

我成功设置了“ Azure AD代理流”,我的Web api安全操作调用和ms graph api调用也都可以工作。 不,我添加了更多与power bi相关的赠款。我想从Web API读取/写入工作区/报告等 我尝试过:

string[] scopes = { "Capacity.Read.All", "Capacity.ReadWrite.All",
    "Content.Create", " Dashboard.Read.All", " Dashboard.ReadWrite.All",
    "Data.Alter_Any", "Dataset.Read.All", "Dataset.ReadWrite.All", "Group.Read", "Group.Read.All",
    "Metadata.View_Any", "Report.Read.All", "Report.ReadWrite.All", "Tenant.Read.All",
    "Workspace.Read.All", "Workspace.ReadWrite.All"};
string accessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUser(HttpContext, scopes); // error
var tokenCredentials = new TokenCredentials(accessToken, "Bearer");
using (var client = new PowerBIClient(new Uri(_powerBiConfig.ApiUrl), tokenCredentials))
{
...
}

但GetAccessTokenOnBehalfOfUser返回

  

AADSTS70011:提供的请求必须包含“作用域”输入   参数。输入参数'scope'的提供值不是   有效。

1 个答案:

答案 0 :(得分:0)

亲自搞定。

下面的代码演示了如何检索所有power bi工作区

public async Task<string> Groups()
{
    string[] scopes = { "https://analysis.windows.net/powerbi/api/Dataset.Read.All"};
    try
    {
        string accessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUser(HttpContext, scopes);
        var tokenCredentials = new TokenCredentials(accessToken, "Bearer");
        using (var client = new PowerBIClient(new Uri(_powerBiConfig.ApiUrl), tokenCredentials))
        {
            return JsonConvert.SerializeObject(client.Groups.GetGroups().Value, Formatting.Indented);
        }
    }
    catch (Exception exc)
    {
        return string.Empty;
    }
}