// This function can be removed if you do not need to support IE const acquireTokenRedirectAndCallMSGraph = () => { //Call acquireTokenSilent (iframe) to obtain a token for Microsoft Graph userAgentApplication.acquireTokenSilent(applicationConfig.graphScopes).then(function (accessToken) { callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); }, function (error) { console.log(error) //Call acquireTokenRedirect in case of acquireToken Failure if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1 || error.indexOf("login_required") !== -1) { userAgentApplication.acquireTokenRedirect(applicationConfig.graphScopes) } }); }
function setTokenRedirectToLocalStorage(req) { auth.acquireTokenSilent(req) .then(function(response) { window.localStorage.setItem("token", response.accessToken); }) .catch(function(error) { // }); }
const getAccessToken = async () => { // Get the access token silently // If the cache contains a non-expired token, this function // will just return the cached token. Otherwise, it will // make a request to the Azure OAuth endpoint to get a token try { const token = window.localStorage.getItem("token"); if (!token) { const response = await auth.acquireTokenSilent(GRAPH_REQUESTS.LOGIN); return response.accessToken; } return token; } catch (error) { if (requiresInteraction(error.errorCode)) { return auth.acquireTokenRedirect(GRAPH_REQUESTS.LOGIN); } else { console.error("Non-interactive error:", error.errorCode); } } }
// Sample API call const acquireTokenPopupAndCallMSGraph = () => { //Call acquireTokenSilent (iframe) to obtain a token for Microsoft Graph userAgentApplication.acquireTokenSilent(applicationConfig.graphScopes).then(function (accessToken) { callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); }, function (error) { console.log(error); // Call acquireTokenPopup (popup window) in case of acquireTokenSilent failure due to consent or interaction required ONLY if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1 || error.indexOf("login_required") !== -1) { userAgentApplication.acquireTokenPopup(applicationConfig.graphScopes).then(function (accessToken) { callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); }, function (error) { console.log(error); }); } }); }