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

Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

var token = new CkJsonObject();
success = token.LoadFile("gmail-token.json");
if (success !== true) {
    console.log("Could not read gmail-token.json from the private working directory.");
    return;
}

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

var http = new CkHttp();
http.RequireSslCertVerify = true;
http.AuthToken = token.StringOf("access_token");
var resp = new CkHttpResponse();
var reply = new CkJsonObject();
success = http.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5",resp);
if (resp.StatusCode == 0) {
    console.log("Gmail did not return an HTTP response.");
    return;
}

if ((success !== true) || (resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
    console.log("Gmail HTTP " + resp.StatusCode);
    success = reply.Load(resp.BodyStr);
    if (success == true) {
        console.log(reply.StringOf("error.message"));
    }

    return;
}

success = reply.Load(resp.BodyStr);
if (success !== true) {
    console.log("Expected a Gmail JSON response.");
    return;
}

var ids = new CkStringTable();
var count = reply.SizeOfArray("messages");
var i = 0;
while ((i < count)) {
    reply.I = i;
    success = ids.Append(reply.StringOf("messages[i].id"));
    if (success !== true) {
        console.log("Could not retain a message ID.");
        return;
    }

    i = i+1;
}

var url = new CkStringBuilder();
i = 0;
while ((i < ids.Count)) {
    url.SetString("https://gmail.googleapis.com/gmail/v1/users/me/messages/");
    url.Append(ids.StringAt(i));
    url.Append("?format=metadata&metadataHeaders=From&metadataHeaders=Subject&metadataHeaders=Date");
    success = http.HttpNoBody("GET",url.GetAsString(),resp);
    if (resp.StatusCode == 0) {
        console.log("Gmail did not return an HTTP response.");
        return;
    }

    if ((success !== true) || (resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
        console.log("Gmail HTTP " + resp.StatusCode);
        success = reply.Load(resp.BodyStr);
        if (success == true) {
            console.log(reply.StringOf("error.message"));
        }

        return;
    }

    success = reply.Load(resp.BodyStr);
    if (success !== true) {
        console.log("Expected a Gmail JSON response.");
        return;
    }

    console.log("Message: " + ids.StringAt(i));
    var h = 0;
    while ((h < reply.SizeOfArray("payload.headers"))) {
        reply.I = h;
        console.log(reply.StringOf("payload.headers[i].name") + ": " + reply.StringOf("payload.headers[i].value"));
        h = h+1;
    }

    i = i+1;
}

console.log("Messages inspected: " + ids.Count);