【问题标题】:can't get power Bi access token无法获得电力 Bi 访问令牌
【发布时间】:2017-12-13 10:12:36
【问题描述】:

我正在尝试使用 JavaScript/angular 获取访问令牌,但我收到错误 请求的资源上不存在“Access-Control-Allow-Origin”标头,而我正在调用 api使用 Postman 获取响应代码 200 访问令牌。

Postman api call for token

从 Angular 调用 api 时控制台出错,但 响应代码为 200 在网络选项卡中。

error response on console

Response code 200 in network tab

我无法以 Angular 代码获取 Power BI 访问令牌,并且在 Power BI 中没有基于 gui 的身份验证的情况下,没有适当的文档可以访问令牌。

【问题讨论】:

    标签: javascript angular powerbi powerbi-embedded


    【解决方案1】:

    如果您使用的是 C#.Net,您可以查看有关如何获取 Power BI AAD 令牌的官方文档: https://docs.microsoft.com/en-us/power-bi/developer/get-azuread-access-token

    using Microsoft.IdentityModel.Clients.ActiveDirectory;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //Redirect uri must match the redirect_uri used when requesting Authorization code.
        string redirectUri = String.Format("{0}Redirect", Properties.Settings.Default.RedirectUrl);
        string authorityUri = Properties.Settings.Default.AADAuthorityUri;
    
        // Get the auth code
        string code = Request.Params.GetValues(0)[0];
    
        // Get auth token from auth code
        TokenCache TC = new TokenCache();
    
        AuthenticationContext AC = new AuthenticationContext(authorityUri, TC);
        ClientCredential cc = new ClientCredential
            (Properties.Settings.Default.ClientID,
            Properties.Settings.Default.ClientSecret);
    
        AuthenticationResult AR = AC.AcquireTokenByAuthorizationCode(code, new Uri(redirectUri), cc);
    
        //Set Session "authResult" index string to the AuthenticationResult
        Session[_Default.authResultString] = AR;
    
        //Redirect back to Default.aspx
        Response.Redirect("/Default.aspx");
    }
    

    如果您使用任何其他语言,您可以使用合适的 AAD 库(例如,用于 js\node.js 的 ADAL.js)或仅使用相同属性的 AAD 端点的 HTTP 调用。

    【讨论】:

      猜你喜欢
      • 2013-02-09
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 1970-01-01
      相关资源
      最近更新 更多