Classic ASP Requires Chilkat v11.0.0+
Classic ASP
Download a Gmail attachment by ID
See more GMail REST API Examples
Use a Gmail message ID and a body.attachmentId from the same message. Find the attachment ID by inspecting its MIME parts. Decode the returned data as base64url, check the decoded byte count, and save it to an application-chosen path. A part containing body.data can be decoded directly from the full message.
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")
set url = Server.CreateObject("Chilkat.StringBuilder")
success = url.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
success = url.Append("GMAIL_MESSAGE_ID")
success = url.Append("/attachments/")
success = url.Append("GMAIL_ATTACHMENT_ID")
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
If ((reply.JsonTypeOf("data") <> 1) Or (reply.JsonTypeOf("size") <> 2)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected attachment data and a byte count.") & "</pre>"
Response.End
End If
set data = Server.CreateObject("Chilkat.BinData")
If (reply.StringOfEquals("data","",1) <> 1) Then
success = reply.BytesOf("data","base64url",data)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not decode the attachment.") & "</pre>"
Response.End
End If
End If
If (data.NumBytes <> reply.IntOf("size")) 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 & " bytes.") & "</pre>"
%>
</body>
</html>