Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.0.0+

Outlook.com: Refresh Mail OAuth2 Tokens

See more Office365 Examples

Exchange a saved delegated refresh token for a new access token without displaying a browser. Use the same app registration and authority as the original authorization. Save any rotated refresh token and the new lifetime information. On a rejected or malformed response, this example preserves the previous cache; an expired/revoked grant or interaction-required response needs interactive authorization again.

Read and update qa_data/tokens/hotmail.json in a private working directory. Serialize access to the cache; production storage should support atomic replacement and protect credentials. A successful refresh does not enable a disabled mail protocol. Client-credentials tokens have no refresh token; request a new application token instead.

Authorize first: desktop PKCE example. This is delegated background access after consent, not permanently unattended application authentication.

Microsoft reference documentation

Chilkat Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkOAuth2W.h>
#include <C_CkDateTimeW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW token;
    HCkOAuth2W oauth;
    HCkJsonObjectW fresh;
    HCkDateTimeW obtained;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    token = CkJsonObjectW_Create();
    success = CkJsonObjectW_LoadFile(token,L"qa_data/tokens/hotmail.json");
    if (success != TRUE) {
        wprintf(L"Could not read qa_data/tokens/hotmail.json from the private working directory.\n");
        CkJsonObjectW_Dispose(token);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(token,L"refresh_token") != 1) || (CkJsonObjectW_StringOfEquals(token,L"refresh_token",L"",TRUE) == TRUE)) {
        wprintf(L"Authorize the account for offline access first.\n");
        CkJsonObjectW_Dispose(token);
        return;
    }

    oauth = CkOAuth2W_Create();
    CkOAuth2W_putTokenEndpoint(oauth,L"https://login.microsoftonline.com/consumers/oauth2/v2.0/token");
    CkOAuth2W_putClientId(oauth,L"CLIENT_ID");
    //  No client secret for the public desktop registration used by the PKCE example.
    CkOAuth2W_putRefreshToken(oauth,CkJsonObjectW_stringOf(token,L"refresh_token"));
    success = CkOAuth2W_RefreshAccessToken(oauth);
    if (success != TRUE) {
        wprintf(L"Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent.\n");
        CkJsonObjectW_Dispose(token);
        CkOAuth2W_Dispose(oauth);
        return;
    }

    fresh = CkJsonObjectW_Create();
    success = CkJsonObjectW_Load(fresh,CkOAuth2W_accessTokenResponse(oauth));
    if (success != TRUE) {
        wprintf(L"Could not parse the refresh response.\n");
        CkJsonObjectW_Dispose(token);
        CkOAuth2W_Dispose(oauth);
        CkJsonObjectW_Dispose(fresh);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(fresh,L"access_token") != 1) || (CkJsonObjectW_StringOfEquals(fresh,L"access_token",L"",TRUE) == TRUE)) {
        wprintf(L"The refresh response did not contain an access token.\n");
        CkJsonObjectW_Dispose(token);
        CkOAuth2W_Dispose(oauth);
        CkJsonObjectW_Dispose(fresh);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(fresh,L"refresh_token") != 1) || (CkJsonObjectW_StringOfEquals(fresh,L"refresh_token",L"",TRUE) == TRUE)) {
        CkJsonObjectW_UpdateString(fresh,L"refresh_token",CkJsonObjectW_stringOf(token,L"refresh_token"));
    }

    if ((CkJsonObjectW_HasMember(fresh,L"scope") != TRUE) && (CkJsonObjectW_JsonTypeOf(token,L"scope") == 1)) {
        CkJsonObjectW_UpdateString(fresh,L"scope",CkJsonObjectW_stringOf(token,L"scope"));
    }

    //  Persist the rotated refresh token. Retain the previous one only if the response omits it.
    //  Use the new expires_in, not the old response's expiration information.
    obtained = CkDateTimeW_Create();
    CkDateTimeW_SetFromCurrentSystemTime(obtained);
    CkJsonObjectW_UpdateString(fresh,L"obtained_at",CkDateTimeW_getAsTimestamp(obtained,FALSE));
    success = CkJsonObjectW_WriteFile(fresh,L"qa_data/tokens/hotmail.json");
    if (success != TRUE) {
        wprintf(L"Could not save the refreshed credentials.\n");
        CkJsonObjectW_Dispose(token);
        CkOAuth2W_Dispose(oauth);
        CkJsonObjectW_Dispose(fresh);
        CkDateTimeW_Dispose(obtained);
        return;
    }

    wprintf(L"Refreshed the access token.\n");


    CkJsonObjectW_Dispose(token);
    CkOAuth2W_Dispose(oauth);
    CkJsonObjectW_Dispose(fresh);
    CkDateTimeW_Dispose(obtained);

    }