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 Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set token = Server.CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-token.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read gmail-token.json from the private working directory.") & "</pre>"
Response.End
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "An OAuth access token is required.") & "</pre>"
Response.End
End If
set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = token.StringOf("access_token")
set resp = Server.CreateObject("Chilkat.HttpResponse")
set reply = Server.CreateObject("Chilkat.JsonObject")
messageId = "GMAIL_MESSAGE_ID"
filename = "attachment.pdf"
set messageUrl = Server.CreateObject("Chilkat.StringBuilder")
success = messageUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
success = messageUrl.Append(http.UrlEncode(messageId))
set url = Server.CreateObject("Chilkat.StringBuilder")
success = url.Append(messageUrl.GetAsString())
success = url.Append("?format=full")
success = http.HttpNoBody("GET",url.GetAsString(),resp)
If (resp.StatusCode = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( "Gmail did not return an HTTP response.") & "</pre>"
Response.End
End If
If ((success <> 1) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Gmail HTTP " & resp.StatusCode) & "</pre>"
success = reply.Load(resp.BodyStr)
If (success = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( reply.StringOf("error.message")) & "</pre>"
End If
Response.End
End If
success = reply.Load(resp.BodyStr)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected a Gmail JSON response.") & "</pre>"
Response.End
End If
set paths = Server.CreateObject("Chilkat.StringTable")
success = paths.Append("payload")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( paths.LastErrorText) & "</pre>"
Response.End
End If
set part = Server.CreateObject("Chilkat.JsonObject")
set selected = Server.CreateObject("Chilkat.JsonObject")
set childPath = Server.CreateObject("Chilkat.StringBuilder")
matches = 0
p = 0
Do While (p < paths.Count)
success = reply.ObjectOf2(paths.StringAt(p),part)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( reply.LastErrorText) & "</pre>"
Response.End
End If
If (part.StringOfEquals("filename",filename,1) = 1) Then
matches = matches + 1
success = part.MakeCopy(selected)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( part.LastErrorText) & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Matching MIME part: " & paths.StringAt(p)) & "</pre>"
End If
j = 0
Do While (j < part.SizeOfArray("parts"))
success = childPath.SetString(paths.StringAt(p))
success = childPath.Append(".parts[")
success = childPath.AppendInt(j)
success = childPath.Append("]")
success = paths.Append(childPath.GetAsString())
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( paths.LastErrorText) & "</pre>"
Response.End
End If
j = j + 1
Loop
p = p + 1
Loop
If (matches <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected one filename match. Matches: " & matches) & "</pre>"
Response.End
End If
set body = Server.CreateObject("Chilkat.JsonObject")
success = selected.ObjectOf2("body",body)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( selected.LastErrorText) & "</pre>"
Response.End
End If
If (body.JsonTypeOf("size") <> 2) Then
Response.Write "<pre>" & Server.HTMLEncode( "The selected part has no byte count.") & "</pre>"
Response.End
End If
expectedSize = body.IntOf("size")
If (body.JsonTypeOf("attachmentId") = 1) Then
success = url.SetString(messageUrl.GetAsString())
success = url.Append("/attachments/")
success = url.Append(http.UrlEncode(body.StringOf("attachmentId")))
success = http.HttpNoBody("GET",url.GetAsString(),resp)
If (resp.StatusCode = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( "Gmail did not return an HTTP response.") & "</pre>"
Response.End
End If
If ((success <> 1) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Gmail HTTP " & resp.StatusCode) & "</pre>"
success = reply.Load(resp.BodyStr)
If (success = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( reply.StringOf("error.message")) & "</pre>"
End If
Response.End
End If
success = reply.Load(resp.BodyStr)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected a Gmail JSON response.") & "</pre>"
Response.End
End If
success = reply.MakeCopy(body)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( reply.LastErrorText) & "</pre>"
Response.End
End If
End If
If (body.JsonTypeOf("data") <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "The attachment response has no data.") & "</pre>"
Response.End
End If
set data = Server.CreateObject("Chilkat.BinData")
If (body.StringOfEquals("data","",1) <> 1) Then
success = body.BytesOf("data","base64url",data)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( body.LastErrorText) & "</pre>"
Response.End
End If
End If
If (data.NumBytes <> expectedSize) Then
Response.Write "<pre>" & Server.HTMLEncode( "Attachment size mismatch.") & "</pre>"
Response.End
End If
success = data.WriteFile("downloaded-attachment.bin")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not save the attachment.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Saved " & data.NumBytes & " attachment bytes.") & "</pre>"
%>
</body>
</html>