Cloud Developer: Office 365 REST API returns 401 access denied with an error code of “invalid_token”


In recent times i worked with this cloud developer (Azure AD, Office 365 REST API) where they created a custom application. It uses (Calendar, Contact, Mail APIs) to pull items from the specified user mailbox. Intermittently they see “401 Access Denied” followed by error code “invalid_token” from the API. During the investigation, we noticed that after 60 minutes the access token becomes invalid.

Native Application to Web API Diagram

So we recommended them to follow the pattern to cache the access token and use it to call the APIs until it return 401. When they get the 401 event, their app should use the refresh token to get a new access token and retry the request. It worked like a charm for the customer.

Note: When the access token expires, the client application will receive an error that indicates the user needs to authenticate again. If the application has a valid refresh token, it can be used to acquire a new access token without prompting the user to sign in again. If the refresh token expires, the application will need to interactively authenticate the user once again. You can refer: https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-authentication-scenarios

Hope this helps.

Comments (6)
  1. Lance says:

    What's up to every , as I am really eager of reading this blog's post to be updated on a regular basis.
    It contains fastidious information.

  2. Twila says:

    Having read this I thought it was rather informative.
    I appreciate you taking the time and energy to put this information together.
    I once again find myself personally spending a significant amount of time both
    reading and posting comments. But so what, it was still worth it!

  3. Home page says:

    My brother suggested I might like this blog. He was totally right.
    This put up truly made my day. You can not imagine simply how much
    time I had spent for this info! Thanks!

  4. district 7 vincity prices says:

    Hmm is anyone else encountering problems with the images on this blog loading?
    I'm trying to determine if its a problem on my end or if it's the blog.
    Any responses would be greatly appreciated.

    1. Deva [MSFT] says:

      It works for me. Can you check it now?

  5. Kiersten says:

    Thiis iis a toplic which iss near too my heart... Besst wishes!

    Where arre youur conttact details though? Greetingys frm Idaho!
    I'm bored tto tears att work sso I decided too browse you sijte oon mmy iphone dduring lunch break.
    I really lie tthe information yoou pdesent heere andd can't waitt too take a look when I get home.
    I'm surprised aat howw quick youir blpog loadeed
    onn mmy phone .. I'm noot een using WIFI, just 3G .. Anyhow, fantasttic site!

    Hi, I do believe ths iis a great site. I stumbldupon it 😉 I ill recisit yet again siknce i
    hace savewd as a favorite it. Money aand freedom iis the bes wayy tto change, mmay yyou
    be ich aand continuee tto help others.

Comments are closed.

Skip to main content