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

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

set token = CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-token.json")
If (success <> 1) Then
    outFile.WriteLine("Could not read gmail-token.json from the private working directory.")
    WScript.Quit
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
    outFile.WriteLine("An OAuth access token is required.")
    WScript.Quit
End If

set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = token.StringOf("access_token")
set resp = CreateObject("Chilkat.HttpResponse")
set reply = CreateObject("Chilkat.JsonObject")
total = 0
set baseUrl = CreateObject("Chilkat.StringBuilder")
success = baseUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=100")
set url = CreateObject("Chilkat.StringBuilder")
set pageToken = CreateObject("Chilkat.StringBuilder")
more = 1
Do While (more = 1)
    success = url.SetString(baseUrl.GetAsString())
    If (pageToken.Length > 0) Then
        success = url.Append("&pageToken=")
        success = url.Append(http.UrlEncode(pageToken.GetAsString()))
    End If

    success = http.HttpNoBody("GET",url.GetAsString(),resp)
    If (resp.StatusCode = 0) Then
        outFile.WriteLine("Gmail did not return an HTTP response.")
        WScript.Quit
    End If

    If ((success <> 1) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
        outFile.WriteLine("Gmail HTTP " & resp.StatusCode)
        success = reply.Load(resp.BodyStr)
        If (success = 1) Then
            outFile.WriteLine(reply.StringOf("error.message"))
        End If

        WScript.Quit
    End If

    success = reply.Load(resp.BodyStr)
    If (success <> 1) Then
        outFile.WriteLine("Expected a Gmail JSON response.")
        WScript.Quit
    End If

    '  Gmail can omit messages when the search or mailbox is empty.
    count = reply.SizeOfArray("messages")
    i = 0
    Do While (i < count)
        reply.I = i
        outFile.WriteLine(reply.StringOf("messages[i].id") & "  thread " & reply.StringOf("messages[i].threadId"))
        total = total + 1
        i = i + 1
    Loop
    more = 0
    If (reply.JsonTypeOf("nextPageToken") = 1) Then
        success = pageToken.SetString(reply.StringOf("nextPageToken"))
        more = pageToken.Length > 0
    End If

Loop
outFile.WriteLine("Messages listed: " & total)

outFile.Close