Home

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

%3CLINGO-SUB%20id%3D%22lingo-sub-1254861%22%20slang%3D%22es-ES%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%22es-ES%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%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%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%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%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%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%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A682%3A16%3C%2FA%3E)%20at%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eat%20new%20Promise%20()%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%20at%20new%20Promise%20()%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A682%3A16%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%20at%20new%20Promise%20()%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%20at%20new%20Promise%20()%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A682%3A16%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%20at%20new%20Promise%20()%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A682%3A16%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%20at%20new%20Promise%20()%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A682%3A16%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%20at%20new%20Promise%20()%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A682%3A16%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%20at%20new%20Promise%20()%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A682%3A16%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%20at%20new%20Promise%20()%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A682%3A16%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%20at%20new%20Promise%20()%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxxx-web-part.js%3A682%3A16%20at%20https%3A%2F%2Flocalhost%3A4321%2Fdist%2Fxxx-web-part.js%3A2916%3A103%20at%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%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%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%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%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%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%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%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%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%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fspoprod-a.akamaihd.net%2Ffiles%2Fsp-client%2Fsp-webpart-workbench-assembly_en-us_27e01b6941bf5cdddecc695bd3bdbb95.js%3A21%3A585352%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%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%3E%3CPRE%3E%3CSPAN%20class%3D%22kwd%22%3Eexport%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3Edefault%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3Eclass%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22typ%22%3EMyWebPart%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3Eextends%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22typ%22%3EBaseClientSideWebPart%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%26lt%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EIWebpartProps%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%26gt%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%7B%3C%2FSPAN%3E%0A%0A%20%20%3CSPAN%20class%3D%22kwd%22%3Epublic%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20render%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E()%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%7B%3C%2FSPAN%3E%0A%0A%20%20%3CSPAN%20class%3D%22kwd%22%3Econst%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20config%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%7B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20auth%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%7B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20%20%20%20%20clientId%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22xxxxxxxxxxxxxxx%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%2C%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20%20%20%20%20authority%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22str%22%3E%22https%3A%2F%2Flogin.microsoftonline.com%2Fyyyyyyyyyyyyyyyyyyy%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%3CSPAN%20class%3D%22pun%22%3E%7D%3C%2FSPAN%3E%0A%20%20%3CSPAN%20class%3D%22pun%22%3E%7D%3B%3C%2FSPAN%3E%0A%0A%20%20%3CSPAN%20class%3D%22kwd%22%3Econst%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20myMSALObj%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22kwd%22%3Enew%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22typ%22%3EUserAgentApplication%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Econfig%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3B%3C%2FSPAN%3E%0A%0A%20%20%3CSPAN%20class%3D%22kwd%22%3Elet%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20accessTokenRequest%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%7B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20scopes%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%5B%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E%22user.read%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%5D%3C%2FSPAN%3E%0A%20%20%3CSPAN%20class%3D%22pun%22%3E%7D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20myMSALObj%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EacquireTokenSilent%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EaccessTokenRequest%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E).%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3Ethen%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3Efunction%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EaccessTokenResponse%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%7B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%3CSPAN%20class%3D%22com%22%3E%2F%2F%20Acquire%20token%20silent%20success%3C%2FSPAN%3E%0A%20%20%20%20%20%20%3CSPAN%20class%3D%22com%22%3E%2F%2F%20call%20API%20with%20token%3C%2FSPAN%3E%0A%20%20%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Elet%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20accessToken%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20accessTokenResponse%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EaccessToken%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Elet%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20scopes%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20accessTokenResponse%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Escopes%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3B%3C%2FSPAN%3E%0A%20%20%3CSPAN%20class%3D%22pun%22%3E%7D).%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3Ecatch%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3Efunction%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eerror%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%7B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22com%22%3E%2F%2FAcquire%20token%20silent%20failure%2C%20and%20send%20an%20interactive%20request%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20%20%20%20%20console%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Elog%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eerror%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Eif%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eerror%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EerrorMessage%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EindexOf%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22str%22%3E%22interaction_required%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E!%3D%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E-%3C%2FSPAN%3E%3CSPAN%20class%3D%22lit%22%3E1%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E%7B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20myMSALObj%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EacquireTokenRedirect%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EaccessTokenRequest%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22pun%22%3E%7D%3C%2FSPAN%3E%0A%20%20%3CSPAN%20class%3D%22pun%22%3E%7D)%3B%3C%2FSPAN%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1254861%22%20slang%3D%22es-ES%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharepoint%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%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
Highlighted
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