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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Token
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Reply
string ls_MessageId
string ls_Filename
oleobject loo_MessageUrl
oleobject loo_Url
oleobject loo_Paths
oleobject loo_Part
oleobject loo_Selected
oleobject loo_ChildPath
long li_Matches
long p
long j
oleobject loo_Body
long li_ExpectedSize
oleobject loo_Data
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Token
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Token.LoadFile("gmail-token.json")
if li_Success <> 1 then
Write-Debug "Could not read gmail-token.json from the private working directory."
destroy loo_Token
return
end if
if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
Write-Debug "An OAuth access token is required."
destroy loo_Token
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = loo_Token.StringOf("access_token")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")
ls_MessageId = "GMAIL_MESSAGE_ID"
ls_Filename = "attachment.pdf"
loo_MessageUrl = create oleobject
li_rc = loo_MessageUrl.ConnectToNewObject("Chilkat.StringBuilder")
loo_MessageUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
loo_MessageUrl.Append(loo_Http.UrlEncode(ls_MessageId))
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")
loo_Url.Append(loo_MessageUrl.GetAsString())
loo_Url.Append("?format=full")
li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "Gmail did not return an HTTP response."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
Write-Debug "Gmail HTTP " + string(loo_Resp.StatusCode)
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success = 1 then
Write-Debug loo_Reply.StringOf("error.message")
end if
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
return
end if
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a Gmail JSON response."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
return
end if
loo_Paths = create oleobject
li_rc = loo_Paths.ConnectToNewObject("Chilkat.StringTable")
li_Success = loo_Paths.Append("payload")
if li_Success <> 1 then
Write-Debug loo_Paths.LastErrorText
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
return
end if
loo_Part = create oleobject
li_rc = loo_Part.ConnectToNewObject("Chilkat.JsonObject")
loo_Selected = create oleobject
li_rc = loo_Selected.ConnectToNewObject("Chilkat.JsonObject")
loo_ChildPath = create oleobject
li_rc = loo_ChildPath.ConnectToNewObject("Chilkat.StringBuilder")
li_Matches = 0
p = 0
do while (p < loo_Paths.Count)
li_Success = loo_Reply.ObjectOf2(loo_Paths.StringAt(p),loo_Part)
if li_Success <> 1 then
Write-Debug loo_Reply.LastErrorText
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
return
end if
if loo_Part.StringOfEquals("filename",ls_Filename,1) = 1 then
li_Matches = li_Matches + 1
li_Success = loo_Part.MakeCopy(loo_Selected)
if li_Success <> 1 then
Write-Debug loo_Part.LastErrorText
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
return
end if
Write-Debug "Matching MIME part: " + loo_Paths.StringAt(p)
end if
j = 0
do while (j < loo_Part.SizeOfArray("parts"))
loo_ChildPath.SetString(loo_Paths.StringAt(p))
loo_ChildPath.Append(".parts[")
loo_ChildPath.AppendInt(j)
loo_ChildPath.Append("]")
li_Success = loo_Paths.Append(loo_ChildPath.GetAsString())
if li_Success <> 1 then
Write-Debug loo_Paths.LastErrorText
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
return
end if
j = j + 1
loop
p = p + 1
loop
if li_Matches <> 1 then
Write-Debug "Expected one filename match. Matches: " + string(li_Matches)
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
return
end if
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Selected.ObjectOf2("body",loo_Body)
if li_Success <> 1 then
Write-Debug loo_Selected.LastErrorText
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
destroy loo_Body
return
end if
if loo_Body.JsonTypeOf("size") <> 2 then
Write-Debug "The selected part has no byte count."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
destroy loo_Body
return
end if
li_ExpectedSize = loo_Body.IntOf("size")
if loo_Body.JsonTypeOf("attachmentId") = 1 then
loo_Url.SetString(loo_MessageUrl.GetAsString())
loo_Url.Append("/attachments/")
loo_Url.Append(loo_Http.UrlEncode(loo_Body.StringOf("attachmentId")))
li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "Gmail did not return an HTTP response."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
destroy loo_Body
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
Write-Debug "Gmail HTTP " + string(loo_Resp.StatusCode)
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success = 1 then
Write-Debug loo_Reply.StringOf("error.message")
end if
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
destroy loo_Body
return
end if
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a Gmail JSON response."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
destroy loo_Body
return
end if
li_Success = loo_Reply.MakeCopy(loo_Body)
if li_Success <> 1 then
Write-Debug loo_Reply.LastErrorText
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
destroy loo_Body
return
end if
end if
if loo_Body.JsonTypeOf("data") <> 1 then
Write-Debug "The attachment response has no data."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
destroy loo_Body
return
end if
loo_Data = create oleobject
li_rc = loo_Data.ConnectToNewObject("Chilkat.BinData")
if loo_Body.StringOfEquals("data","",1) <> 1 then
li_Success = loo_Body.BytesOf("data","base64url",loo_Data)
if li_Success <> 1 then
Write-Debug loo_Body.LastErrorText
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
destroy loo_Body
destroy loo_Data
return
end if
end if
if loo_Data.NumBytes <> li_ExpectedSize then
Write-Debug "Attachment size mismatch."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
destroy loo_Body
destroy loo_Data
return
end if
li_Success = loo_Data.WriteFile("downloaded-attachment.bin")
if li_Success <> 1 then
Write-Debug "Could not save the attachment."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
destroy loo_Body
destroy loo_Data
return
end if
Write-Debug "Saved " + string(loo_Data.NumBytes) + " attachment bytes."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_MessageUrl
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_Selected
destroy loo_ChildPath
destroy loo_Body
destroy loo_Data