Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkOAuth2.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkOAuth2 oauth;
    const char *authUrl;
    int waited;
    HCkJsonObject token;

    success = FALSE;

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

    oauth = CkOAuth2_Create();
    CkOAuth2_putTokenEndpoint(oauth,"https://oauth2.googleapis.com/token");
    CkOAuth2_putClientId(oauth,"YOUR_DESKTOP_CLIENT_ID");
    CkOAuth2_putClientSecret(oauth,"YOUR_DESKTOP_CLIENT_SECRET");

    CkOAuth2_putAuthorizationEndpoint(oauth,"https://accounts.google.com/o/oauth2/v2/auth");
    CkOAuth2_putLocalHost(oauth,"127.0.0.1");
    CkOAuth2_putListenPort(oauth,55568);
    CkOAuth2_putListenPortRangeEnd(oauth,55578);
    CkOAuth2_putCodeChallenge(oauth,TRUE);
    CkOAuth2_putCodeChallengeMethod(oauth,"S256");
    CkOAuth2_putScope(oauth,"https://www.googleapis.com/auth/gmail.readonly");
    success = CkOAuth2_AddAuthQueryParam(oauth,"access_type","offline");
    if (success != TRUE) {
        printf("Could not configure offline access.\n");
        CkOAuth2_Dispose(oauth);
        return;
    }

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

    authUrl = CkOAuth2_startAuth(oauth);
    if (CkOAuth2_getLastMethodSuccess(oauth) != TRUE) {
        printf("Could not start the loopback listener.\n");
        CkOAuth2_Dispose(oauth);
        return;
    }

    success = CkOAuth2_LaunchBrowser(oauth,authUrl);
    if (success != TRUE) {
        CkOAuth2_Cancel(oauth);
        printf("Could not open the system browser.\n");
        CkOAuth2_Dispose(oauth);
        return;
    }

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

    if (CkOAuth2_getAuthFlowState(oauth) < 3) {
        CkOAuth2_Cancel(oauth);
        printf("Authorization timed out.\n");
        CkOAuth2_Dispose(oauth);
        return;
    }

    if (CkOAuth2_getAuthFlowState(oauth) != 3) {
        printf("Authorization did not complete. Flow state: %d\n",CkOAuth2_getAuthFlowState(oauth));
        CkOAuth2_Dispose(oauth);
        return;
    }

    token = CkJsonObject_Create();
    success = CkJsonObject_Load(token,CkOAuth2_accessTokenResponse(oauth));
    if (success != TRUE) {
        printf("Could not parse the token response.\n");
        CkOAuth2_Dispose(oauth);
        CkJsonObject_Dispose(token);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(token,"refresh_token") != 1) || (CkJsonObject_StringOfEquals(token,"refresh_token","",TRUE) == TRUE)) {
        printf("No offline refresh token was issued. The existing cache was preserved.\n");
        CkOAuth2_Dispose(oauth);
        CkJsonObject_Dispose(token);
        return;
    }

    success = CkJsonObject_WriteFile(token,"gmail-token.json");
    if (success != TRUE) {
        printf("Could not save the token file.\n");
        CkOAuth2_Dispose(oauth);
        CkJsonObject_Dispose(token);
        return;
    }

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


    CkOAuth2_Dispose(oauth);
    CkJsonObject_Dispose(token);

    }