获取PowerBI Embedded的Azure PowerBI容量的授权代码

时间:2018-11-13 06:12:38

标签: asp.net-mvc azure powerbi

我正在以编程方式启动/停止PowerBI Embedded的Azure PowerBI容量。

在按钮上单击,在Azure中恢复/暂停powerbi嵌入服务。我按照下面的链接进行操作。

https://docs.microsoft.com/en-us/rest/api/power-bi-embedded/capacities/resume

每次单击按钮时如何动态获取授权代码。

1 个答案:

答案 0 :(得分:1)

您可以使用Azure Active Directory Authentication Libraries获得Power BI的访问令牌。最简单的方法是安装Microsoft.IdentityModel.Clients.ActiveDirectory NuGet程序包。然后,要获取访问令牌,您需要调用AcquireTokenAsync方法。这是您可以执行的操作:

    private static string redirectUri = "https://login.live.com/oauth20_desktop.srf";
    private static string resourceUri = "https://analysis.windows.net/powerbi/api";
    private static string authorityUri = "https://login.windows.net/common/oauth2/authorize";
    // Obtain at https://dev.powerbi.com/apps
    private static string clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

    private static AuthenticationContext authContext = new AuthenticationContext(authorityUri, new TokenCache());

    private async void btnAuthenticate_ClickAsync(object sender, EventArgs e)
    {
        var authenticationResult = await authContext.AcquireTokenAsync(resourceUri, clientId, new Uri(redirectUri), new PlatformParameters(PromptBehavior.Auto));
        if (authenticationResult == null)
            MessageBox.Show("Call failed.");
        else
            MessageBox.Show(authenticationResult.AccessToken);
    }

最后一个参数是PromptBehavior.Auto。这意味着,除非您的身份保存在此计算机上,否则系统将提示您输入凭据。另外,如果未获得此应用的访问许可,也会提示用户。身份验证以交互方式执行-期望有人来输入凭据,以备不时之需。如果要以非交互方式获取访问令牌,则可以在代码中使用用户名和密码。在这种情况下,获取访问令牌的方法应如下所示:

    private void btnAuthenticate_Click(object sender, EventArgs e)
    {
        AuthenticationResult authenticationResult = null;

        // First check is there token in the cache
        try
        {
            authenticationResult = authContext.AcquireTokenSilentAsync(resourceUri, clientId).Result;
        }
        catch (AggregateException ex)
        {
            AdalException ex2 = ex.InnerException as AdalException;
            if ((ex2 == null) || (ex2 != null && ex2.ErrorCode != "failed_to_acquire_token_silently"))
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        if (authenticationResult == null)
        {
            var uc = new UserPasswordCredential("user@example.com", "<EnterStrongPasswordHere>"); // Or parameterless if you want to use Windows integrated auth
            try
            {
                authenticationResult = authContext.AcquireTokenAsync(resourceUri, clientId, uc).Result;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.InnerException == null ? "" : Environment.NewLine + ex.InnerException.Message);
                return;
            }
        }

        if (authenticationResult == null)
            MessageBox.Show("Call failed.");
        else
            MessageBox.Show(authenticationResult.AccessToken);
    }

请注意,如果未同意您的应用,则此调用可能会失败。为此,请转到Azure门户-> Azure Active Directory->应用程序注册,然后找到您的应用程序。然后打开应用程序的设置,然后在“必需的权限”中选择Power BI Service,然后单击“授予权限”: enter image description here

这时,您可以使用此访问令牌执行REST API调用或在应用程序中嵌入元素。使用此令牌可以访问用户可以访问的所有内容,并且当您在门户中注册应用程序时,就可以访问该令牌。但是,如果您想为一个特定的报告(或图块或仪表板)生成令牌,则可以调用某些Embed Token方法,例如GenerateTokenInGroup(使用ADAL访问令牌在用于生成嵌入式令牌的请求的标头中对自己进行身份验证)。