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

List Gmail message IDs across all pages

See more GMail REST API Examples

List message and thread IDs for the authenticated user. Continue with nextPageToken until it is absent. A message ID is a Gmail identifier, distinct from the RFC Message-ID header. Use it in the reading examples. Spam and Trash are excluded by default.

Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.

Google documentation

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkJsonObjectW token;
    success = token.LoadFile(L"gmail-token.json");
    if (success != true) {
        wprintf(L"Could not read gmail-token.json from the private working directory.\n");
        return;
    }

    if ((token.JsonTypeOf(L"access_token") != 1) || (token.StringOfEquals(L"access_token",L"",true) == true)) {
        wprintf(L"An OAuth access token is required.\n");
        return;
    }

    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(token.stringOf(L"access_token"));
    CkHttpResponseW resp;
    CkJsonObjectW reply;
    int total = 0;
    CkStringBuilderW baseUrl;
    baseUrl.Append(L"https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=100");
    CkStringBuilderW url;
    CkStringBuilderW pageToken;
    bool more = true;
    while ((more == true)) {
        url.SetString(baseUrl.getAsString());
        if (pageToken.get_Length() > 0) {
            url.Append(L"&pageToken=");
            url.Append(http.urlEncode(pageToken.getAsString()));
        }

        success = http.HttpNoBody(L"GET",url.getAsString(),resp);
        if (resp.get_StatusCode() == 0) {
            wprintf(L"Gmail did not return an HTTP response.\n");
            return;
        }

        if ((success != true) || (resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
            wprintf(L"Gmail HTTP %d\n",resp.get_StatusCode());
            success = reply.Load(resp.bodyStr());
            if (success == true) {
                wprintf(L"%s\n",reply.stringOf(L"error.message"));
            }

            return;
        }

        success = reply.Load(resp.bodyStr());
        if (success != true) {
            wprintf(L"Expected a Gmail JSON response.\n");
            return;
        }

        //  Gmail can omit messages when the search or mailbox is empty.
        int count = reply.SizeOfArray(L"messages");
        int i = 0;
        while ((i < count)) {
            reply.put_I(i);
            wprintf(L"%s  thread %s\n",reply.stringOf(L"messages[i].id"),reply.stringOf(L"messages[i].threadId"));
            total = total + 1;
            i = i + 1;
        }

        more = false;
        if (reply.JsonTypeOf(L"nextPageToken") == 1) {
            pageToken.SetString(reply.stringOf(L"nextPageToken"));
            more = pageToken.get_Length() > 0;
        }

    }

    wprintf(L"Messages listed: %d\n",total);
    }