16

I want to integrate with Miscrosoft Outlook. I am able to login with MSAL.js and get an access token, but I am not able to get a refresh token. Is there a way to do it?

3 Answers 3

11

I'll assume that since you're using the MSAL.js (https://github.com/AzureAD/microsoft-authentication-library-for-js) that you're using implicit flow for authentication and authorization.

Implicit flow doesn't support refresh tokens, but you can request a new token silently. This is done similarly to how you request the token (id or access) in the first place. Unfortunately, I haven't found that MSAL.js does this transparently and I've needed to detect expired tokens and request the new tokens in my code. You can read more about refreshing tokens here.

Alternatively, if what you're implementing allows you to use one of the other MSAL libraries (for example, the .Net one) then you can use one of the other OAuth flows that explicitly support refresh tokens.

1
  • 1
    thanks for the answer @nbrowne. Can you review this part of ur answer: "Unfortunately, I haven't found that MSAL.js does this transparently.." Commented Jan 3, 2018 at 22:36
4

I couldn't find any answer in the MSAL.js documentation, however this source code comment suggests you can renew a token manually by passing only the clientId as your scope to acquireTokenSilent.

To renew idToken, please pass clientId as the only scope in the Authentication Parameters

9
  • @AaronFriedman this comment is correct but I had to set up something to manually do this on a timer or listening to the browser onLine events. Commented Sep 23, 2020 at 20:27
  • When you set this up manually, do you retrieve an access token as well? Commented Sep 24, 2020 at 17:56
  • 1
    Yes, I do a double token acquisition. Do the silent token with the client ID and then another with the regular scope. Commented Sep 24, 2020 at 17:58
  • So just to verify, the scopes should just look like: ['client_id'] Commented Sep 24, 2020 at 18:01
  • 1
    It worked. My problem was that I was using the npm package @azure/msal-angular. I switched to the msal package then reimplemented the auth for my project. After implementing a solution for the msal package release version, I was able to get the silent id_token renewal working. Thanks! Commented Sep 26, 2020 at 19:56
3

I use msal v1.4.0

I remove 2 keys in storage (see picture) then call acquireTokenSilent again to get new access token. enter image description here Code to remove those 2 keys:

const keys = Object.keys(sessionStorage).filter(x => x.indexOf('authority') > 0)
keys.forEach(x => sessionStorage.removeItem(x))
6
  • Can exchange sessionStorage for localStorage
    – PeterM
    Commented Oct 2, 2020 at 22:07
  • In version 1.4.2 this tends to occasionally throw "cannot_parse_cache"
    – PeterM
    Commented Oct 27, 2020 at 23:21
  • I don't have issues with 1.4.2. You can try by downloading a sample app from Microsoft at github.com/Azure-Samples/… Change the library in index.html to 1.4.2 (it's still 1.4.0 now) then add the codes to remove 2 keys in getTokenPopup() before calling acquireTokenSilent()
    – qnguyen
    Commented Oct 28, 2020 at 11:29
  • @PeterMark This got fixed in 1.4.3!
    – sandrooco
    Commented Nov 3, 2020 at 8:28
  • 1
    @yesman create an instance: const myMSALObj = new Msal.UserAgentApplication(msalConfig) then myMSALObj.acquireTokenSilent(request). You can see a sample project at github.com/Azure-Samples/… There are examples for popup and redirect in JavaScriptSPA folder (authPopup.js and authRedirect.js)
    – qnguyen
    Commented Nov 26, 2020 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.