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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

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

  final token = CkJsonObject();
  success = true;
  try {
    token.loadFile('gmail-token.json');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not read gmail-token.json from the private working directory.');
    return;
  }

  if ((token.jsonTypeOf('access_token') != 1) || (token.stringOfEquals('access_token', '', true))) {
    print('An OAuth access token is required.');
    return;
  }

  final http = CkHttp();
  http.requireSslCertVerify = true;
  http.authToken = token.stringOf('access_token');
  final resp = CkHttpResponse();
  final reply = CkJsonObject();
  success = true;
  try {
    http.httpNoBody('GET', 'https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5', resp);
  } on ChilkatException {
    success = false;
  }
  if (resp.statusCode == 0) {
    print('Gmail did not return an HTTP response.');
    return;
  }

  if ((!success) || ((resp.statusCode < 200) || (resp.statusCode >= 300))) {
    print('Gmail HTTP ${resp.statusCode}');
    success = true;
    try {
      reply.load(resp.bodyStr);
    } on ChilkatException {
      success = false;
    }
    if (success) {
      print(reply.stringOf('error.message'));
    }

    return;
  }

  success = true;
  try {
    reply.load(resp.bodyStr);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Expected a Gmail JSON response.');
    return;
  }

  final ids = CkStringTable();
  final count = reply.sizeOfArray('messages');
  var i = 0;
  while (i < count) {
    reply.i = i;
    success = true;
    try {
      ids.append(reply.stringOf('messages[i].id'));
    } on ChilkatException {
      success = false;
    }
    if (!success) {
      print('Could not retain a message ID.');
      return;
    }

    i++;
  }

  final url = 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 = true;
    try {
      http.httpNoBody('GET', url.getAsString(), resp);
    } on ChilkatException {
      success = false;
    }
    if (resp.statusCode == 0) {
      print('Gmail did not return an HTTP response.');
      return;
    }

    if ((!success) || ((resp.statusCode < 200) || (resp.statusCode >= 300))) {
      print('Gmail HTTP ${resp.statusCode}');
      success = true;
      try {
        reply.load(resp.bodyStr);
      } on ChilkatException {
        success = false;
      }
      if (success) {
        print(reply.stringOf('error.message'));
      }

      return;
    }

    success = true;
    try {
      reply.load(resp.bodyStr);
    } on ChilkatException {
      success = false;
    }
    if (!success) {
      print('Expected a Gmail JSON response.');
      return;
    }

    print('Message: ${ids.stringAt(i)}');
    var h = 0;
    while (h < reply.sizeOfArray('payload.headers')) {
      reply.i = h;
      print('${reply.stringOf('payload.headers[i].name')}: ${reply.stringOf('payload.headers[i].value')}');
      h++;
    }

    i++;
  }

  print('Messages inspected: ${ids.count}');
}