【问题标题】:power bi access token api working fine in postman but not working in angular apppower bi access token api在邮递员中工作正常,但在角度应用程序中不工作
【发布时间】:2019-10-22 07:12:12
【问题描述】:

我在邮递员中调用 power bi API,它运行良好,并且作为我得到的公认结果。 当我在角度应用程序中调用该应用程序时,它不起作用。 我的响应为空。

我在下面附上了我的代码, 请帮帮我,我最近几天一直在尝试解决这个问题。

API: https://login.microsoftonline.com/common/oauth2/token

下面是我的角码

getAccessToken(param): Observable<any> {

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type': 'application/x-www-form-urlencoded',
  })
};

const body = new HttpParams()
  .set('resource', 'https://analysis.windows.net/powerbi/api')
  .set('client_id', 'xxxxxxxxx')
  .set('client_secret', 'xxxxxxxxx')
  .set('grant_type', 'password')
  .set('scope', 'openid')
  .set('username', 'xxxxxxxxx')
  .set('password', 'xxxxxxxxx');

return this.http.post("https://login.microsoftonline.com/common/oauth2/token", body.toString(), httpOptions)
  .pipe(
    map(response => response)
  );}

在下面的参考图像中,相同的 API 运行良好,但在 Angular 中无法运行。

【问题讨论】:

    标签: angular http-post powerbi postman powerbi-embedded


    【解决方案1】:

    您将正文作为 http 参数发布。改为:

    const body = {
        resource: 'https://analysis.windows.net/powerbi/api',
        client_id: 'xxxxxxxxx',
        client_secret: 'xxxxxxxxx',
        grant_type: 'password',
        scope: 'openid',
        username: 'xxxxxxxxx',
        password: 'xxxxxxxxx'
    }
    
    return this.http.post("https://login.microsoftonline.com/common/oauth2/token", body, httpOptions)
      .pipe(
        map(response => response)
      );}
    

    【讨论】:

    • 嗨 Chrillewoodz,我按照您的建议进行了尝试,但收到错误“400 Bad Request”
    • @MitulPatel 他们期望字符串作为正文吗?
    • 我尝试使用“HttpParams”,它的状态为 200(成功)但响应为空
    • @MitulPatel 你能给我一个你正在关注的文档的链接吗?
    【解决方案2】:

    您可以像这样设置标题和正文:

    const httpOptions = {
        headers: new HttpHeaders().set('Content-Type', 'application/x-www-form- 
            urlencoded')
    };
    
    const params = {
        resource: 'https://analysis.windows.net/powerbi/api',
        client_id: 'xxxxxxxxx',
        client_secret: 'xxxxxxxxx',
        grant_type: 'password',
        scope: 'openid',
        username: 'xxxxxxxxx',
        password: 'xxxxxxxxx'
    }
    
    const body = `resource=${params.resource}&client_id=${params.client_id}&client_secret=${params.client_secret}&grant_type=${params.grant_type}& scope =${params.scope}&username =${params.username}&password =${params.password}`;
    
    return this.http.post("https://login.microsoftonline.com/common/oauth2/token", 
        body, httpOptions)
    .pipe(
        map(response => response)
    );
    

    【讨论】:

    • 你好 Banki,还是同样的问题,我按照你的建议进行了更改,我得到了状态代码:200 并且响应为空
    • 是的,我遇到了 CORS 错误,为了解决我在 chrome 中安装了 CORS 扩展,所以现在我没有收到 cors 错误
    【解决方案3】:

    我得到了问题和解决方案。

    问题:
    为什么它破坏了我的 API 响应“null”。 问题是由于 CORS,我使用了 crome 的 CORS 扩展,它已被弃用。 所以它的结果是'null'

    解决方案:
    我为 chrome 使用了最新的一个 CORS 扩展,这暂时解决了这个问题。 这不是一个合适的解决方案,因为我们无权在后端解决 cors 问题。 如果我们需要自己解决这个问题,那么我们必须在 .net 中创建 API 以获取 AccessToken,然后我们可以在 Angular 中使用该 API。

    【讨论】:

      猜你喜欢
      • 2020-08-27
      • 2017-08-16
      • 2020-05-25
      • 1970-01-01
      • 2018-02-25
      • 2020-08-18
      • 1970-01-01
      • 2020-01-28
      • 2019-05-27
      相关资源
      最近更新 更多