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

Inspect the MIME parts of a Gmail message

See more GMail REST API Examples

Supply a Gmail message ID and request format=full. Walk the root payload and every nested parts array, including a single-part root. For each part, show its MIME type, filename, and size. A body.attachmentId can be passed to the attachment download example. Inline body.data is already included as base64url.

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 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")
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
$oUrl.Append("GMAIL_MESSAGE_ID")
$oUrl.Append("?format=full")
$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

If ($oReply.JsonTypeOf("payload") <> 3) Then
    ConsoleWrite("Expected a MIME payload." & @CRLF)
    Exit
EndIf

;  Queue paths into the original JSON document so any MIME nesting depth is supported.
$oPaths = ObjCreate("Chilkat.StringTable")
$bSuccess = $oPaths.Append("payload")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not queue the root part." & @CRLF)
    Exit
EndIf

$oPart = ObjCreate("Chilkat.JsonObject")
$oChildPath = ObjCreate("Chilkat.StringBuilder")
Local $iP = 0
While ($iP < $oPaths.Count)
    $bSuccess = $oReply.ObjectOf2($oPaths.StringAt($iP),$oPart)
    If ($bSuccess <> True) Then
        ConsoleWrite("Could not read a MIME part." & @CRLF)
        Exit
    EndIf

    ConsoleWrite("Part: " & $oPart.StringOf("mimeType") & "  bytes: " & $oPart.IntOf("body.size") & @CRLF)
    If ($oPart.JsonTypeOf("filename") = 1) Then
        If ($oPart.StringOfEquals("filename","",True) <> True) Then
            ConsoleWrite("Filename: " & $oPart.StringOf("filename") & @CRLF)
        EndIf

    EndIf

    If ($oPart.JsonTypeOf("body.attachmentId") = 1) Then
        ConsoleWrite("Attachment ID: " & $oPart.StringOf("body.attachmentId") & @CRLF)
    Else
        If ($oPart.JsonTypeOf("body.data") = 1) Then
            ConsoleWrite("Body data is available in this part." & @CRLF)
        EndIf

    EndIf

Local $iJ = 0
    While ($iJ < $oPart.SizeOfArray("parts"))
        $oChildPath.SetString($oPaths.StringAt($iP))
        $oChildPath.Append(".parts[")
        $oChildPath.AppendInt($iJ)
        $oChildPath.Append("]")
        $bSuccess = $oPaths.Append($oChildPath.GetAsString())
        If ($bSuccess <> True) Then
            ConsoleWrite("Could not queue a child part." & @CRLF)
            Exit
        EndIf

        $iJ = $iJ + 1
    Wend
    $iP = $iP + 1
Wend