Search code examples
angularhttp-postpowerbipostmanpowerbi-embedded

power bi access token api working fine in postman but not working in angular app


I'm calling power bi API in postman which is working good and as an accepted result I'm getting. while that app I'm calling in the angular app it's not working. I'm getting response null.

I have attached my code below, Please help me out I have been trying to solve this issue last few days.

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

Below is my angular code

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

In the below reference image same API which is working good but not working in angular.

enter image description here


Solution

  • I got the issue and solution.

    Issue:
    why it's broken my API response 'null'. The issue is that due to CORS, I used the CORS extension for crome it was deprecated. so it was given the result 'null'

    Solution:
    I used the latest one CORS extension for chrome and that's resolved the issue temporarily. this is not a proper solution because we don't have the right to resolved cors issue in the backend. if we need to solve it form our hand then we have to create API in .net for getting AccessToken and then that API we can consume in Angular.