获取PowerBI Embedded的Azure PowerBI容量的授权代码 [英] Get Authorization code for Azure PowerBI capacity for PowerBI Embedded

查看:248
本文介绍了获取PowerBI Embedded的Azure PowerBI容量的授权代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在按钮上单击,恢复/暂停Powerbi嵌入服务在Azure中。我遵循下面的链接来执行此操作。





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


I'm programatically start/stop Azure PowerBI capacity for PowerBI Embedded.

On button click , resume/suspend the powerbi embed service in Azure. I followed below link to do this.

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

How to get authorization code dynamicallly each time i click the button.

解决方案

You can get an access token for Power BI using Azure Active Directory Authentication Libraries. The easiest way to get it is to install Microsoft.IdentityModel.Clients.ActiveDirectory NuGet package. Then to obtain an access token you need to call AcquireTokenAsync method. Here is how you can do this:

    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);
    }

The last parameter is PromptBehavior.Auto. This means that you will be prompted for credentials, unless your identity is saved on this computer. Also, when there is no consent for access is given this app, the user will be prompted too. The authentication is performed in an interactive way - it expect that there will be a human, who will enter credentials in case they are needed. If you want to obtain an access token in non-interactive way, you can use user name and password in your code. In this case the method for obtaining the access token should look like this:

    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);
    }

Please note, that this call may fail, if no consent is given to your app. To do this, go to Azure Portal -> Azure Active Directory -> App registrations and locate your app. Then open your app's settings and in Required permissions select Power BI Service and click Grant permissions:

At this point you can use this access token to perform REST API calls or to embed elements in your app. This token gives access to everything which the user can access and it has been allowed to be accessed when you registered your app in the portal. If you want however to generate a token for one particular report (or tile, or dashboard), then you can call some of the Embed Token methods, e.g. GenerateTokenInGroup (using the ADAL access token to authenticate yourself in the headers of the request for generating the embedded token).

这篇关于获取PowerBI Embedded的Azure PowerBI容量的授权代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆