Download a Gmail attachment by filename
See more GMail REST API Examples
Start with a Gmail message ID and the exact filename. Walk the root MIME payload and all nested parts. A unique matching part can contain base64url data directly or an attachment ID for a second request. Duplicate filenames are reported so the application can choose a particular MIME part.
The output path is chosen locally. The example saves to downloaded-attachment.bin and verifies the decoded size. Use an appropriate application-chosen filename or extension when adapting it. Gmail’s filename is treated as a matching value.
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.
Chilkat AutoIt Downloads
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")
Local $sMessageId = "GMAIL_MESSAGE_ID"
Local $sFilename = "attachment.pdf"
$oMessageUrl = ObjCreate("Chilkat.StringBuilder")
$oMessageUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
$oMessageUrl.Append($oHttp.UrlEncode($sMessageId))
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oUrl.Append($oMessageUrl.GetAsString())
$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
$oPaths = ObjCreate("Chilkat.StringTable")
$bSuccess = $oPaths.Append("payload")
If ($bSuccess <> True) Then
ConsoleWrite($oPaths.LastErrorText & @CRLF)
Exit
EndIf
$oPart = ObjCreate("Chilkat.JsonObject")
$oSelected = ObjCreate("Chilkat.JsonObject")
$oChildPath = ObjCreate("Chilkat.StringBuilder")
Local $iMatches = 0
Local $iP = 0
While ($iP < $oPaths.Count)
$bSuccess = $oReply.ObjectOf2($oPaths.StringAt($iP),$oPart)
If ($bSuccess <> True) Then
ConsoleWrite($oReply.LastErrorText & @CRLF)
Exit
EndIf
If ($oPart.StringOfEquals("filename",$sFilename,True) = True) Then
$iMatches = $iMatches + 1
$bSuccess = $oPart.MakeCopy($oSelected)
If ($bSuccess <> True) Then
ConsoleWrite($oPart.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Matching MIME part: " & $oPaths.StringAt($iP) & @CRLF)
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($oPaths.LastErrorText & @CRLF)
Exit
EndIf
$iJ = $iJ + 1
Wend
$iP = $iP + 1
Wend
If ($iMatches <> 1) Then
ConsoleWrite("Expected one filename match. Matches: " & $iMatches & @CRLF)
Exit
EndIf
$oBody = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oSelected.ObjectOf2("body",$oBody)
If ($bSuccess <> True) Then
ConsoleWrite($oSelected.LastErrorText & @CRLF)
Exit
EndIf
If ($oBody.JsonTypeOf("size") <> 2) Then
ConsoleWrite("The selected part has no byte count." & @CRLF)
Exit
EndIf
Local $iExpectedSize = $oBody.IntOf("size")
If ($oBody.JsonTypeOf("attachmentId") = 1) Then
$oUrl.SetString($oMessageUrl.GetAsString())
$oUrl.Append("/attachments/")
$oUrl.Append($oHttp.UrlEncode($oBody.StringOf("attachmentId")))
$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
$bSuccess = $oReply.MakeCopy($oBody)
If ($bSuccess <> True) Then
ConsoleWrite($oReply.LastErrorText & @CRLF)
Exit
EndIf
EndIf
If ($oBody.JsonTypeOf("data") <> 1) Then
ConsoleWrite("The attachment response has no data." & @CRLF)
Exit
EndIf
$oData = ObjCreate("Chilkat.BinData")
If ($oBody.StringOfEquals("data","",True) <> True) Then
$bSuccess = $oBody.BytesOf("data","base64url",$oData)
If ($bSuccess <> True) Then
ConsoleWrite($oBody.LastErrorText & @CRLF)
Exit
EndIf
EndIf
If ($oData.NumBytes <> $iExpectedSize) Then
ConsoleWrite("Attachment size mismatch." & @CRLF)
Exit
EndIf
$bSuccess = $oData.WriteFile("downloaded-attachment.bin")
If ($bSuccess <> True) Then
ConsoleWrite("Could not save the attachment." & @CRLF)
Exit
EndIf
ConsoleWrite("Saved " & $oData.NumBytes & " attachment bytes." & @CRLF)