Forum Discussion
vcima
Mar 26, 2020Brass Contributor
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 (https://spoprod-a.akamaihd.net/files/sp-client/sp-webpart-workbench-assembly_en-us_27e01b6941bf5cdddecc695bd3bdbb95.js:21:585834) at https://spoprod-a.akamaihd.net/files/sp-client/sp-webpart-workbench-assembly_en-us_27e01b6941bf5cdddecc695bd3bdbb95.js:21:585352
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); } });
- Sudharsan KSteel ContributorHi,
Please look at the below post for detail
https://techcommunity.microsoft.com/t5/sharepoint-developer/how-can-i-auto-logged-an-user-inside-a-sharepoint-web-part/m-p/1248512#M8774