0

From angular application i want to call https://login.microsoftonline.com/##tenant##/oauth2/v2.0/token api to get access token from http call and using that token i want to call https://graph.microsoft.com/v1.0/users/##UserId##​​​​​​​​​​​​​/getMemberGroups API without using any npm package of angular.

I tried using http service, but getting below error

Access to XMLHttpRequest at 'https://login.microsoftonline.com/xxxx/oauth2/v2.0/token' from origin 'https://xxx.co' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Is there any another way to call Graph API for token and user get login user groups ?

1
  • Try to see the implicit grant flow to get access token. Commented Dec 23, 2020 at 13:56

1 Answer 1

2

It is not allowed to call https://login.microsoftonline.com/<tenant>/oauth2/v2.0/token directly from a signal page application due to CORS. You should use MSAL for angular to do this. My code is based on this angular official demo.

Pls go to src/app/app.module.ts to config your Azure Ad:

enter image description here

and run this demo first.

After you run this demo successfully,go to profile.component.ts, add codes below:

  getAccessTokenAndCallGraphAPI(){

    this.authService.acquireTokenPopup({
      scopes: ['GroupMember.Read.All']
    }).then(result=>{
      console.log("access token: "+ result.accessToken)

      const httpOptions = {
        headers: new HttpHeaders({
          'Content-Type':  'application/json',
          Authorization: 'Bearer '+result.accessToken
        })}

      const reqBody = {
        "securityEnabledOnly": true
      }
      this.http.post("https://graph.microsoft.com/v1.0/users/<user you want to query>/getMemberGroups",reqBody,httpOptions).toPromise().then(result=>{console.log(result)});
    })
  }

Call this function when init page:

ngOnInit() {
    this.getProfile();
    this.getAccessTokenAndCallGraphAPI();
  }

result:

enter image description here

Let me know if you have any other questions.

Sign up to request clarification or add additional context in comments.

1 Comment

An argument for 'interceptorConfig' was not provided. This error occurs in app.module with the above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.