Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

asp.net mvc - Get Authorization code for Azure PowerBI capacity for PowerBI Embedded

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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: enter image description here

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...