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

Refresh a Gmail OAuth access token

See more GMail REST API Examples

Use the refresh token and the same Desktop app client that issued it. Run in the private directory used by the authorization example. Preserve the refresh token if Google omits it from the response. Refreshing continues the existing grant. Request additional scopes through interactive authorization.

Google documentation

Chilkat Unicode C Downloads

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

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

    success = FALSE;

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

    token = CkJsonObjectW_Create();
    success = CkJsonObjectW_LoadFile(token,L"gmail-token.json");
    if (success != TRUE) {
        wprintf(L"Could not read gmail-token.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://oauth2.googleapis.com/token");
    CkOAuth2W_putClientId(oauth,L"YOUR_DESKTOP_CLIENT_ID");
    CkOAuth2W_putClientSecret(oauth,L"YOUR_DESKTOP_CLIENT_SECRET");
    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"));
    }

    success = CkJsonObjectW_WriteFile(fresh,L"gmail-token.json");
    if (success != TRUE) {
        wprintf(L"Could not save the refreshed credentials.\n");
        CkJsonObjectW_Dispose(token);
        CkOAuth2W_Dispose(oauth);
        CkJsonObjectW_Dispose(fresh);
        return;
    }

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


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

    }