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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoToken
Handle hoHttp
Variant vResp
Handle hoResp
Handle hoReply
String sMessageId
String sFilename
Handle hoMessageUrl
Handle hoUrl
Handle hoPaths
Variant vPart
Handle hoPart
Variant vSelected
Handle hoSelected
Handle hoChildPath
Integer iMatches
Integer p
Integer j
Variant vBody
Handle hoBody
Integer iExpectedSize
Variant vData
Handle hoData
String sTemp1
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatJsonObject)) To hoToken
If (Not(IsComObjectCreated(hoToken))) Begin
Send CreateComObject of hoToken
End
Get ComLoadFile Of hoToken "gmail-token.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read gmail-token.json from the private working directory."
Procedure_Return
End
Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "An OAuth access token is required."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Get ComStringOf Of hoToken "access_token" To sTemp1
Set ComAuthToken Of hoHttp To sTemp1
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get Create (RefClass(cComChilkatJsonObject)) To hoReply
If (Not(IsComObjectCreated(hoReply))) Begin
Send CreateComObject of hoReply
End
Move "GMAIL_MESSAGE_ID" To sMessageId
Move "attachment.pdf" To sFilename
Get Create (RefClass(cComChilkatStringBuilder)) To hoMessageUrl
If (Not(IsComObjectCreated(hoMessageUrl))) Begin
Send CreateComObject of hoMessageUrl
End
Get ComAppend Of hoMessageUrl "https://gmail.googleapis.com/gmail/v1/users/me/messages/" To iSuccess
Get ComUrlEncode Of hoHttp sMessageId To sTemp1
Get ComAppend Of hoMessageUrl sTemp1 To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
If (Not(IsComObjectCreated(hoUrl))) Begin
Send CreateComObject of hoUrl
End
Get ComGetAsString Of hoMessageUrl To sTemp1
Get ComAppend Of hoUrl sTemp1 To iSuccess
Get ComAppend Of hoUrl "?format=full" To iSuccess
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "Gmail did not return an HTTP response."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Gmail HTTP " iTemp1
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iSuccess
If (iSuccess = True) Begin
Get ComStringOf Of hoReply "error.message" To sTemp1
Showln sTemp1
End
Procedure_Return
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a Gmail JSON response."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringTable)) To hoPaths
If (Not(IsComObjectCreated(hoPaths))) Begin
Send CreateComObject of hoPaths
End
Get ComAppend Of hoPaths "payload" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoPaths To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoPart
If (Not(IsComObjectCreated(hoPart))) Begin
Send CreateComObject of hoPart
End
Get Create (RefClass(cComChilkatJsonObject)) To hoSelected
If (Not(IsComObjectCreated(hoSelected))) Begin
Send CreateComObject of hoSelected
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoChildPath
If (Not(IsComObjectCreated(hoChildPath))) Begin
Send CreateComObject of hoChildPath
End
Move 0 To iMatches
Move 0 To p
While (p < (ComCount(hoPaths)))
Get ComStringAt Of hoPaths p To sTemp1
Get pvComObject of hoPart to vPart
Get ComObjectOf2 Of hoReply sTemp1 vPart To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoReply To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOfEquals Of hoPart "filename" sFilename True To bTemp1
If (bTemp1 = True) Begin
Move (iMatches + 1) To iMatches
Get pvComObject of hoSelected to vSelected
Get ComMakeCopy Of hoPart vSelected To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoPart To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringAt Of hoPaths p To sTemp1
Showln "Matching MIME part: " sTemp1
End
Move 0 To j
While (j < (ComSizeOfArray(hoPart, "parts")))
Get ComStringAt Of hoPaths p To sTemp1
Get ComSetString Of hoChildPath sTemp1 To iSuccess
Get ComAppend Of hoChildPath ".parts[" To iSuccess
Get ComAppendInt Of hoChildPath j To iSuccess
Get ComAppend Of hoChildPath "]" To iSuccess
Get ComGetAsString Of hoChildPath To sTemp1
Get ComAppend Of hoPaths sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoPaths To sTemp1
Showln sTemp1
Procedure_Return
End
Move (j + 1) To j
Loop
Move (p + 1) To p
Loop
If (iMatches <> 1) Begin
Showln "Expected one filename match. Matches: " iMatches
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoBody
If (Not(IsComObjectCreated(hoBody))) Begin
Send CreateComObject of hoBody
End
Get pvComObject of hoBody to vBody
Get ComObjectOf2 Of hoSelected "body" vBody To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoSelected To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComJsonTypeOf Of hoBody "size" To iTemp1
If (iTemp1 <> 2) Begin
Showln "The selected part has no byte count."
Procedure_Return
End
Get ComIntOf Of hoBody "size" To iExpectedSize
Get ComJsonTypeOf Of hoBody "attachmentId" To iTemp1
If (iTemp1 = 1) Begin
Get ComGetAsString Of hoMessageUrl To sTemp1
Get ComSetString Of hoUrl sTemp1 To iSuccess
Get ComAppend Of hoUrl "/attachments/" To iSuccess
Get ComUrlEncode Of hoHttp (ComStringOf(hoBody, "attachmentId")) To sTemp1
Get ComAppend Of hoUrl sTemp1 To iSuccess
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "Gmail did not return an HTTP response."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Gmail HTTP " iTemp1
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iSuccess
If (iSuccess = True) Begin
Get ComStringOf Of hoReply "error.message" To sTemp1
Showln sTemp1
End
Procedure_Return
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a Gmail JSON response."
Procedure_Return
End
Get pvComObject of hoBody to vBody
Get ComMakeCopy Of hoReply vBody To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoReply To sTemp1
Showln sTemp1
Procedure_Return
End
End
Get ComJsonTypeOf Of hoBody "data" To iTemp1
If (iTemp1 <> 1) Begin
Showln "The attachment response has no data."
Procedure_Return
End
Get Create (RefClass(cComChilkatBinData)) To hoData
If (Not(IsComObjectCreated(hoData))) Begin
Send CreateComObject of hoData
End
Get ComStringOfEquals Of hoBody "data" "" True To bTemp1
If (bTemp1 <> True) Begin
Get pvComObject of hoData to vData
Get ComBytesOf Of hoBody "data" "base64url" vData To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoBody To sTemp1
Showln sTemp1
Procedure_Return
End
End
Get ComNumBytes Of hoData To iTemp1
If (iTemp1 <> iExpectedSize) Begin
Showln "Attachment size mismatch."
Procedure_Return
End
Get ComWriteFile Of hoData "downloaded-attachment.bin" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not save the attachment."
Procedure_Return
End
Get ComNumBytes Of hoData To iTemp1
Showln "Saved " iTemp1 " attachment bytes."
End_Procedure