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

%3CLINGO-SUB%20id%3D%22lingo-sub-1264419%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20get%20an%20accessToken%20using%20MSAL%20library%20inside%20a%20Sharepoint%20Web%20Part%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1264419%22%20slang%3D%22en-US%22%3EHi%2C%3CBR%20%2F%3EPlease%20look%20at%20the%20below%20post%20for%20detail%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fsharepoint-developer%2Fhow-can-i-auto-logged-an-user-inside-a-sharepoint-web-part%2Fm-p%2F1248512%23M8774%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fsharepoint-developer%2Fhow-can-i-auto-logged-an-user-inside-a-sharepoint-web-part%2Fm-p%2F1248512%23M8774%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1254861%22%20slang%3D%22en-US%22%3EHow%20to%20get%20an%20accessToken%20using%20MSAL%20library%20inside%20a%20Sharepoint%20Web%20Part%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1254861%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20developed%20a%20Sharepoint%20Web%20Part%20where%20I%20need%20to%20obtain%20the%20accessToken.%20To%20obtain%20the%20token%20I%20have%20used%20a%20MSAL%20library.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20problem%20is%20the%20next%20one%3A%20I'm%20logged%20in%20my%20Sharepoint%20but%20when%20the%20Web%20Part%20try%20to%20retrieve%20the%20accessToken%20something%20fails%20in%20the%20authentication%20and%20appears%20this%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Exxxx-web-part.js%3A1838%20ClientAuthError%3A%20User%20login%20is%20required.%20at%20ClientAuthError.AuthError%20%5Bas%20constructor%5D%20(%3CA%20href%3D%22https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2057%3A28%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2057%3A28%3C%2FA%3E)%20at%20new%20ClientAuthError%20(%3CA%20href%3D%22https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A630%3A28%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A630%3A28%3C%2FA%3E)%20at%20Function.7ZR7.ClientAuthError.createUserLoginRequiredError%20(%3CA%20href%3D%22https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A682%3A16%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A682%3A16%3C%2FA%3E)%20at%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%3C%2FA%3E%26nbsp%3Bat%20new%20Promise%20()%20at%20UserAgentApplication.ZES5.UserAgentApplication.acquireTokenSilent%20(%3CA%20href%3D%22https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A2905%3A16%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A2905%3A16%3C%2FA%3E)%20at%20UserAgentApplication.descriptor.value%20%5Bas%20acquireTokenSilent%5D%20(%3CA%20href%3D%22https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A2543%3A38%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A2543%3A38%3C%2FA%3E)%20at%20xxxWebPart.PlTk.xxxxWebPart.render%20(%3CA%20href%3D%22https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A1828%3A19%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A1828%3A19%3C%2FA%3E)%20at%20xxxxWebPart.t._renderWithAccessibleTitle%20(%3CA%20href%3D%22https%3A%2F%2Fspoprod-a.akamaihd.net%2Ffiles%2Fsp-client%2Fsp-webpart-workbench-assembly_en-us_27e01b6941bf5cdddecc695bd3bdbb95.js%3A21%3A585834%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fspoprod-a.akamaihd.net%2Ffiles%2Fsp-client%2Fsp-webpart-workbench-assembly_en-us_27e01b6941bf5cdddecc695bd3bdbb95.js%3A21%3A585834%3C%2FA%3E)%20at%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fspoprod-a.akamaihd.net%2Ffiles%2Fsp-client%2Fsp-webpart-workbench-assembly_en-us_27e01b6941bf5cdddecc695bd3bdbb95.js%3A21%3A585352%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fspoprod-a.akamaihd.net%2Ffiles%2Fsp-client%2Fsp-webpart-workbench-assembly_en-us_27e01b6941bf5cdddecc695bd3bdbb95.js%3A21%3A585352%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20can%20fix%20this%20problem%3F.%20I%20would%20like%20that%20if%20I'm%20logged%20in%20Sharepoint%20obtain%20the%20accessToken%20without%20relogin%20into%20the%20system.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20my%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3Eexport%20default%20class%20MyWebPart%20extends%20BaseClientSideWebPart%20%3CIWEBPARTPROPS%3E%20%7B%20public%20render()%3A%20void%20%7B%20const%20config%20%3D%20%7B%20auth%3A%20%7B%20clientId%3A%20%22xxxxxxxxxxxxxxx%22%2C%20authority%3A%20%22%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2Fyyyyyyyyyyyyyyyyyyy%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2Fyyyyyyyyyyyyyyyyyyy%3C%2FA%3E%22%20%7D%20%7D%3B%20const%20myMSALObj%20%3D%20new%20UserAgentApplication(config)%3B%20let%20accessTokenRequest%20%3D%20%7B%20scopes%3A%20%5B%22user.read%22%5D%20%7D%20myMSALObj.acquireTokenSilent(accessTokenRequest).then(function(accessTokenResponse)%20%7B%20%2F%2F%20Acquire%20token%20silent%20success%20%2F%2F%20call%20API%20with%20token%20let%20accessToken%20%3D%20accessTokenResponse.accessToken%3B%20let%20scopes%20%3D%20accessTokenResponse.scopes%3B%20%7D).catch(function%20(error)%20%7B%20%2F%2FAcquire%20token%20silent%20failure%2C%20and%20send%20an%20interactive%20request%20console.log(error)%3B%20if%20(error.errorMessage.indexOf(%22interaction_required%22)%20!%3D%3D%20-1)%20%7B%20myMSALObj.acquireTokenRedirect(accessTokenRequest)%3B%20%7D%20%7D)%3B%3C%2FIWEBPARTPROPS%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1254861%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3C%2FLINGO-BODY%3E
Contributor

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 (https://spoprod-a.akamaihd.net/files/sp-client/sp-webpart-workbench-assembly_en-us_27e01b6941bf5cddd...) at https://spoprod-a.akamaihd.net/files/sp-client/sp-webpart-workbench-assembly_en-us_27e01b6941bf5cddd...

 

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: "https://login.microsoftonline.com/yyyyyyyyyyyyyyyyyyy"
      }
  };

  const myMSALObj = new UserAgentApplication(config);

  let accessTokenRequest = {      scopes: ["user.read"]
  }  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