How to get an accessToken using MSAL library inside a Sharepoint Web Part?


I have developed a Sharepoint Web Part where I need to obtain the accessToken. To obtain the token I have used a MSAL library.


My problem is the next one: I'm logged in my Sharepoint but when the Web Part try to retrieve the accessToken something fails in the authentication and appears this error:


xxxx-web-part.js:1838 ClientAuthError: User login is required. at ClientAuthError.AuthError [as constructor] (https://localhost:4321/dist/xxx-web-part.js:2057:28) at new ClientAuthError (https://localhost:4321/dist/xxxx-web-part.js:630:28) at Function.7ZR7.ClientAuthError.createUserLoginRequiredError (https://localhost:4321/dist/xxxx-web-part.js:682:16) at https://localhost:4321/dist/xxx-web-part.js:2916:103 at new Promise () at UserAgentApplication.ZES5.UserAgentApplication.acquireTokenSilent (https://localhost:4321/dist/xxxx-web-part.js:2905:16) at UserAgentApplication.descriptor.value [as acquireTokenSilent] (https://localhost:4321/dist/xxxx-web-part.js:2543:38) at xxxWebPart.PlTk.xxxxWebPart.render (https://localhost:4321/dist/xxxx-web-part.js:1828:19) at xxxxWebPart.t._renderWithAccessibleTitle ( at


How can fix this problem?. I would like that if I'm logged in Sharepoint obtain the accessToken without relogin into the system.


This is my code:


export default class MyWebPart extends BaseClientSideWebPart <IWebpartProps> {

  public render(): void {

  const config = {      auth: {          clientId: "xxxxxxxxxxxxxxx",          authority: ""

  const myMSALObj = new UserAgentApplication(config);

  let accessTokenRequest = {      scopes: [""]
  }  myMSALObj.acquireTokenSilent(accessTokenRequest).then(function(accessTokenResponse) {
      // Acquire token silent success
      // call API with token
      let accessToken = accessTokenResponse.accessToken;
      let scopes = accessTokenResponse.scopes;
  }).catch(function (error) {
          //Acquire token silent failure, and send an interactive request          console.log(error);
          if (error.errorMessage.indexOf("interaction_required") !== -1) {              myMSALObj.acquireTokenRedirect(accessTokenRequest);
1 Reply