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

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.LoadFile("gmail-token.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read gmail-token.json from the private working directory." & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
    ConsoleWrite("An OAuth access token is required." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $oToken.StringOf("access_token")
$oResp = ObjCreate("Chilkat.HttpResponse")
$oReply = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oHttp.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5",$oResp)
If ($oResp.StatusCode = 0) Then
    ConsoleWrite("Gmail did not return an HTTP response." & @CRLF)
    Exit
EndIf

If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
    ConsoleWrite("Gmail HTTP " & $oResp.StatusCode & @CRLF)
    $bSuccess = $oReply.Load($oResp.BodyStr)
    If ($bSuccess = True) Then
        ConsoleWrite($oReply.StringOf("error.message") & @CRLF)
    EndIf

    Exit
EndIf

$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a Gmail JSON response." & @CRLF)
    Exit
EndIf

$oIds = ObjCreate("Chilkat.StringTable")
Local $iCount = $oReply.SizeOfArray("messages")
Local $i = 0
While ($i < $iCount)
    $oReply.I = $i
    $bSuccess = $oIds.Append($oReply.StringOf("messages[i].id"))
    If ($bSuccess <> True) Then
        ConsoleWrite("Could not retain a message ID." & @CRLF)
        Exit
    EndIf

    $i = $i + 1
Wend
$oUrl = ObjCreate("Chilkat.StringBuilder")
$i = 0
While ($i < $oIds.Count)
    $oUrl.SetString("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
    $oUrl.Append($oIds.StringAt($i))
    $oUrl.Append("?format=metadata&metadataHeaders=From&metadataHeaders=Subject&metadataHeaders=Date")
    $bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$oResp)
    If ($oResp.StatusCode = 0) Then
        ConsoleWrite("Gmail did not return an HTTP response." & @CRLF)
        Exit
    EndIf

    If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
        ConsoleWrite("Gmail HTTP " & $oResp.StatusCode & @CRLF)
        $bSuccess = $oReply.Load($oResp.BodyStr)
        If ($bSuccess = True) Then
            ConsoleWrite($oReply.StringOf("error.message") & @CRLF)
        EndIf

        Exit
    EndIf

    $bSuccess = $oReply.Load($oResp.BodyStr)
    If ($bSuccess <> True) Then
        ConsoleWrite("Expected a Gmail JSON response." & @CRLF)
        Exit
    EndIf

    ConsoleWrite("Message: " & $oIds.StringAt($i) & @CRLF)
Local $iH = 0
    While ($iH < $oReply.SizeOfArray("payload.headers"))
        $oReply.I = $iH
        ConsoleWrite($oReply.StringOf("payload.headers[i].name") & ": " & $oReply.StringOf("payload.headers[i].value") & @CRLF)
        $iH = $iH + 1
    Wend
    $i = $i + 1
Wend
ConsoleWrite("Messages inspected: " & $oIds.Count & @CRLF)