Get the Microsoft Graph Token with adal-angular package adal.js authentication context

178 views Asked by At

I am trying to use the authenticationContext created using 'adal-angular/lib/adal.js' package to fetch token for the microsoft graph resources.

Error:

User Login is required. At this point console.log(:::error is here)

Dependencies:

"adal-angular": "^1.0.18",
"vue": "^2.6.12",
"vue-adal": "^1.3",

Code:

initialize() {
  this.authenticationContext = new AuthenticationContext(config);
  return new Promise((resolve, reject) => {
    if (this.authenticationContext.isCallback(window.location.hash) || window.self !== window.top) {
        this.authenticationContext.handleWindowCallback();
        this.graphToken(this.authenticationContext);
        let user = this.authenticationContext.getCachedUser();
        resolve(user);
     } else {
        let user = this.authenticationContext.getCachedUser();
        if (user) {
          resolve(user);
        } else {
          this.signIn();
        }
     }
   });
  }

graphToken(authctx) {
  authctx.acquireToken('https://graph.microsoft.com', function (error, token) {
    console.log(error + ":::error is here");
    console.log(token + "graph token");
  })  
}
1

There are 1 answers

0
sshweta7 On
  initialize() {
this.authenticationContext = new AuthenticationContext(config);

return new Promise((resolve, reject) => {
  if (this.authenticationContext.isCallback(window.location.hash) || window.self !== window.top) {
    // redirect to the location specified in the url params.
    this.authenticationContext.handleWindowCallback();
  }
  // try pull the user out of local storage
  let user = this.authenticationContext.getCachedUser();
  // get token
  auth.acquireToken(accessTokenRequest.scopes).then((accessToken)=> {
    if (user) {
      user.token_access_graph = accessToken;
      resolve(user);
    } else {
      this.signIn();
    }
  }).catch(error => {
    console.log('acquireToken error', error);
  })
});

},