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

Authorize a desktop application to use Gmail

See more GMail REST API Examples

Enable the Gmail API, configure the OAuth consent screen, and create a Desktop app OAuth client. Launch the system browser and receive the authorization code on a loopback listener. The sample requests read-only access with PKCE and offline access. For another operation, select the scope in that example before authorizing. Gmail scopes.

Run in a private, user-only directory. The file contains bearer credentials. In a deployed application use protected credential storage. To reuse an existing grant, copy the older examples’ qa_data/tokens/gmail.json to gmail-token.json in this private directory. The token retains its original scopes. Gmail clients using IMAP, POP, or SMTP with OAuth need the https://mail.google.com/ scope. Use a separate cache for each user and client.

Google documentation

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkOAuth2W oauth;
    const wchar_t *authUrl;
    int waited;
    HCkJsonObjectW token;

    success = FALSE;

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

    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_putAuthorizationEndpoint(oauth,L"https://accounts.google.com/o/oauth2/v2/auth");
    CkOAuth2W_putLocalHost(oauth,L"127.0.0.1");
    CkOAuth2W_putListenPort(oauth,55568);
    CkOAuth2W_putListenPortRangeEnd(oauth,55578);
    CkOAuth2W_putCodeChallenge(oauth,TRUE);
    CkOAuth2W_putCodeChallengeMethod(oauth,L"S256");
    CkOAuth2W_putScope(oauth,L"https://www.googleapis.com/auth/gmail.readonly");
    success = CkOAuth2W_AddAuthQueryParam(oauth,L"access_type",L"offline");
    if (success != TRUE) {
        wprintf(L"Could not configure offline access.\n");
        CkOAuth2W_Dispose(oauth);
        return;
    }

    //  This is the interactive setup step. Subsequent refreshes use the saved refresh token.
    success = CkOAuth2W_AddAuthQueryParam(oauth,L"prompt",L"consent");
    if (success != TRUE) {
        wprintf(L"Could not configure consent.\n");
        CkOAuth2W_Dispose(oauth);
        return;
    }

    authUrl = CkOAuth2W_startAuth(oauth);
    if (CkOAuth2W_getLastMethodSuccess(oauth) != TRUE) {
        wprintf(L"Could not start the loopback listener.\n");
        CkOAuth2W_Dispose(oauth);
        return;
    }

    success = CkOAuth2W_LaunchBrowser(oauth,authUrl);
    if (success != TRUE) {
        CkOAuth2W_Cancel(oauth);
        wprintf(L"Could not open the system browser.\n");
        CkOAuth2W_Dispose(oauth);
        return;
    }

    waited = 0;
    while (((CkOAuth2W_getAuthFlowState(oauth) < 3) && (waited < 180000))) {
        CkOAuth2W_SleepMs(oauth,200);
        waited = waited + 200;
    }

    if (CkOAuth2W_getAuthFlowState(oauth) < 3) {
        CkOAuth2W_Cancel(oauth);
        wprintf(L"Authorization timed out.\n");
        CkOAuth2W_Dispose(oauth);
        return;
    }

    if (CkOAuth2W_getAuthFlowState(oauth) != 3) {
        wprintf(L"Authorization did not complete. Flow state: %d\n",CkOAuth2W_getAuthFlowState(oauth));
        CkOAuth2W_Dispose(oauth);
        return;
    }

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

    if ((CkJsonObjectW_JsonTypeOf(token,L"refresh_token") != 1) || (CkJsonObjectW_StringOfEquals(token,L"refresh_token",L"",TRUE) == TRUE)) {
        wprintf(L"No offline refresh token was issued. The existing cache was preserved.\n");
        CkOAuth2W_Dispose(oauth);
        CkJsonObjectW_Dispose(token);
        return;
    }

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

    wprintf(L"Credentials saved in the private working directory.\n");


    CkOAuth2W_Dispose(oauth);
    CkJsonObjectW_Dispose(token);

    }