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

Read selected headers for a page of Gmail messages

See more GMail REST API Examples

List up to five messages and fetch only From, Subject, and Date for each one. Request metadataHeaders once per header name. Read the returned headers by name because order and presence vary. This header preview leaves the MIME body on Gmail.

Authorize the application with https://www.googleapis.com/auth/gmail.metadata. 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 <CkStringTableW.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;
    success = http.HttpNoBody(L"GET",L"https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5",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;
    }

    CkStringTableW ids;
    int count = reply.SizeOfArray(L"messages");
    int i = 0;
    while ((i < count)) {
        reply.put_I(i);
        success = ids.Append(reply.stringOf(L"messages[i].id"));
        if (success != true) {
            wprintf(L"Could not retain a message ID.\n");
            return;
        }

        i = i + 1;
    }

    CkStringBuilderW url;
    i = 0;
    while ((i < ids.get_Count())) {
        url.SetString(L"https://gmail.googleapis.com/gmail/v1/users/me/messages/");
        url.Append(ids.stringAt(i));
        url.Append(L"?format=metadata&metadataHeaders=From&metadataHeaders=Subject&metadataHeaders=Date");
        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;
        }

        wprintf(L"Message: %s\n",ids.stringAt(i));
        int h = 0;
        while ((h < reply.SizeOfArray(L"payload.headers"))) {
            reply.put_I(h);
            wprintf(L"%s: %s\n",reply.stringOf(L"payload.headers[i].name"),reply.stringOf(L"payload.headers[i].value"));
            h = h + 1;
        }

        i = i + 1;
    }

    wprintf(L"Messages inspected: %d\n",ids.get_Count());
    }