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

编程入门 行业动态 更新时间:2024-10-28 22:24:40
本文介绍了获取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.

docs.microsoft/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 = "login.live/oauth20_desktop.srf"; private static string resourceUri = "analysis.windows/powerbi/api"; private static string authorityUri = "login.windows/common/oauth2/authorize"; // Obtain at dev.powerbi/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", "<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容量的授权代码

本文发布于:2023-10-30 08:20:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容量   代码   PowerBI   Embedded   Azure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!