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 <CkOAuth2W.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkOAuth2W oauth;
    oauth.put_TokenEndpoint(L"https://oauth2.googleapis.com/token");
    oauth.put_ClientId(L"YOUR_DESKTOP_CLIENT_ID");
    oauth.put_ClientSecret(L"YOUR_DESKTOP_CLIENT_SECRET");

    oauth.put_AuthorizationEndpoint(L"https://accounts.google.com/o/oauth2/v2/auth");
    oauth.put_LocalHost(L"127.0.0.1");
    oauth.put_ListenPort(55568);
    oauth.put_ListenPortRangeEnd(55578);
    oauth.put_CodeChallenge(true);
    oauth.put_CodeChallengeMethod(L"S256");
    oauth.put_Scope(L"https://www.googleapis.com/auth/gmail.readonly");
    success = oauth.AddAuthQueryParam(L"access_type",L"offline");
    if (success != true) {
        wprintf(L"Could not configure offline access.\n");
        return;
    }

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

    const wchar_t *authUrl = oauth.startAuth();
    if (oauth.get_LastMethodSuccess() != true) {
        wprintf(L"Could not start the loopback listener.\n");
        return;
    }

    success = oauth.LaunchBrowser(authUrl);
    if (success != true) {
        oauth.Cancel();
        wprintf(L"Could not open the system browser.\n");
        return;
    }

    int waited = 0;
    while (((oauth.get_AuthFlowState() < 3) && (waited < 180000))) {
        oauth.SleepMs(200);
        waited = waited + 200;
    }

    if (oauth.get_AuthFlowState() < 3) {
        oauth.Cancel();
        wprintf(L"Authorization timed out.\n");
        return;
    }

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

    CkJsonObjectW token;
    success = token.Load(oauth.accessTokenResponse());
    if (success != true) {
        wprintf(L"Could not parse the token response.\n");
        return;
    }

    if ((token.JsonTypeOf(L"refresh_token") != 1) || (token.StringOfEquals(L"refresh_token",L"",true) == true)) {
        wprintf(L"No offline refresh token was issued. The existing cache was preserved.\n");
        return;
    }

    success = token.WriteFile(L"gmail-token.json");
    if (success != true) {
        wprintf(L"Could not save the token file.\n");
        return;
    }

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