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 Unicode C Downloads
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkStringTableW.h>
#include <C_CkBinDataW.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObjectW token;
HCkHttpW http;
HCkHttpResponseW resp;
HCkJsonObjectW reply;
const wchar_t *messageId;
const wchar_t *filename;
HCkStringBuilderW messageUrl;
HCkStringBuilderW url;
HCkStringTableW paths;
HCkJsonObjectW part;
HCkJsonObjectW selected;
HCkStringBuilderW childPath;
int matches;
int p;
int j;
HCkJsonObjectW body;
int expectedSize;
HCkBinDataW data;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
token = CkJsonObjectW_Create();
success = CkJsonObjectW_LoadFile(token,L"gmail-token.json");
if (success != TRUE) {
wprintf(L"Could not read gmail-token.json from the private working directory.\n");
CkJsonObjectW_Dispose(token);
return;
}
if ((CkJsonObjectW_JsonTypeOf(token,L"access_token") != 1) || (CkJsonObjectW_StringOfEquals(token,L"access_token",L"",TRUE) == TRUE)) {
wprintf(L"An OAuth access token is required.\n");
CkJsonObjectW_Dispose(token);
return;
}
http = CkHttpW_Create();
CkHttpW_putRequireSslCertVerify(http,TRUE);
CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(token,L"access_token"));
resp = CkHttpResponseW_Create();
reply = CkJsonObjectW_Create();
messageId = L"GMAIL_MESSAGE_ID";
filename = L"attachment.pdf";
messageUrl = CkStringBuilderW_Create();
CkStringBuilderW_Append(messageUrl,L"https://gmail.googleapis.com/gmail/v1/users/me/messages/");
CkStringBuilderW_Append(messageUrl,CkHttpW_urlEncode(http,messageId));
url = CkStringBuilderW_Create();
CkStringBuilderW_Append(url,CkStringBuilderW_getAsString(messageUrl));
CkStringBuilderW_Append(url,L"?format=full");
success = CkHttpW_HttpNoBody(http,L"GET",CkStringBuilderW_getAsString(url),resp);
if (CkHttpResponseW_getStatusCode(resp) == 0) {
wprintf(L"Gmail did not return an HTTP response.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
return;
}
if ((success != TRUE) || (CkHttpResponseW_getStatusCode(resp) < 200) || (CkHttpResponseW_getStatusCode(resp) >= 300)) {
wprintf(L"Gmail HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
success = CkJsonObjectW_Load(reply,CkHttpResponseW_bodyStr(resp));
if (success == TRUE) {
wprintf(L"%s\n",CkJsonObjectW_stringOf(reply,L"error.message"));
}
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
return;
}
success = CkJsonObjectW_Load(reply,CkHttpResponseW_bodyStr(resp));
if (success != TRUE) {
wprintf(L"Expected a Gmail JSON response.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
return;
}
paths = CkStringTableW_Create();
success = CkStringTableW_Append(paths,L"payload");
if (success != TRUE) {
wprintf(L"%s\n",CkStringTableW_lastErrorText(paths));
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
return;
}
part = CkJsonObjectW_Create();
selected = CkJsonObjectW_Create();
childPath = CkStringBuilderW_Create();
matches = 0;
p = 0;
while ((p < CkStringTableW_getCount(paths))) {
success = CkJsonObjectW_ObjectOf2(reply,CkStringTableW_stringAt(paths,p),part);
if (success != TRUE) {
wprintf(L"%s\n",CkJsonObjectW_lastErrorText(reply));
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
return;
}
if (CkJsonObjectW_StringOfEquals(part,L"filename",filename,TRUE) == TRUE) {
matches = matches + 1;
success = CkJsonObjectW_MakeCopy(part,selected);
if (success != TRUE) {
wprintf(L"%s\n",CkJsonObjectW_lastErrorText(part));
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
return;
}
wprintf(L"Matching MIME part: %s\n",CkStringTableW_stringAt(paths,p));
}
j = 0;
while ((j < CkJsonObjectW_SizeOfArray(part,L"parts"))) {
CkStringBuilderW_SetString(childPath,CkStringTableW_stringAt(paths,p));
CkStringBuilderW_Append(childPath,L".parts[");
CkStringBuilderW_AppendInt(childPath,j);
CkStringBuilderW_Append(childPath,L"]");
success = CkStringTableW_Append(paths,CkStringBuilderW_getAsString(childPath));
if (success != TRUE) {
wprintf(L"%s\n",CkStringTableW_lastErrorText(paths));
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
return;
}
j = j + 1;
}
p = p + 1;
}
if (matches != 1) {
wprintf(L"Expected one filename match. Matches: %d\n",matches);
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
return;
}
body = CkJsonObjectW_Create();
success = CkJsonObjectW_ObjectOf2(selected,L"body",body);
if (success != TRUE) {
wprintf(L"%s\n",CkJsonObjectW_lastErrorText(selected));
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
CkJsonObjectW_Dispose(body);
return;
}
if (CkJsonObjectW_JsonTypeOf(body,L"size") != 2) {
wprintf(L"The selected part has no byte count.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
CkJsonObjectW_Dispose(body);
return;
}
expectedSize = CkJsonObjectW_IntOf(body,L"size");
if (CkJsonObjectW_JsonTypeOf(body,L"attachmentId") == 1) {
CkStringBuilderW_SetString(url,CkStringBuilderW_getAsString(messageUrl));
CkStringBuilderW_Append(url,L"/attachments/");
CkStringBuilderW_Append(url,CkHttpW_urlEncode(http,CkJsonObjectW_stringOf(body,L"attachmentId")));
success = CkHttpW_HttpNoBody(http,L"GET",CkStringBuilderW_getAsString(url),resp);
if (CkHttpResponseW_getStatusCode(resp) == 0) {
wprintf(L"Gmail did not return an HTTP response.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
CkJsonObjectW_Dispose(body);
return;
}
if ((success != TRUE) || (CkHttpResponseW_getStatusCode(resp) < 200) || (CkHttpResponseW_getStatusCode(resp) >= 300)) {
wprintf(L"Gmail HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
success = CkJsonObjectW_Load(reply,CkHttpResponseW_bodyStr(resp));
if (success == TRUE) {
wprintf(L"%s\n",CkJsonObjectW_stringOf(reply,L"error.message"));
}
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
CkJsonObjectW_Dispose(body);
return;
}
success = CkJsonObjectW_Load(reply,CkHttpResponseW_bodyStr(resp));
if (success != TRUE) {
wprintf(L"Expected a Gmail JSON response.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
CkJsonObjectW_Dispose(body);
return;
}
success = CkJsonObjectW_MakeCopy(reply,body);
if (success != TRUE) {
wprintf(L"%s\n",CkJsonObjectW_lastErrorText(reply));
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
CkJsonObjectW_Dispose(body);
return;
}
}
if (CkJsonObjectW_JsonTypeOf(body,L"data") != 1) {
wprintf(L"The attachment response has no data.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
CkJsonObjectW_Dispose(body);
return;
}
data = CkBinDataW_Create();
if (CkJsonObjectW_StringOfEquals(body,L"data",L"",TRUE) != TRUE) {
success = CkJsonObjectW_BytesOf(body,L"data",L"base64url",data);
if (success != TRUE) {
wprintf(L"%s\n",CkJsonObjectW_lastErrorText(body));
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
CkJsonObjectW_Dispose(body);
CkBinDataW_Dispose(data);
return;
}
}
if (CkBinDataW_getNumBytes(data) != expectedSize) {
wprintf(L"Attachment size mismatch.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
CkJsonObjectW_Dispose(body);
CkBinDataW_Dispose(data);
return;
}
success = CkBinDataW_WriteFile(data,L"downloaded-attachment.bin");
if (success != TRUE) {
wprintf(L"Could not save the attachment.\n");
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
CkJsonObjectW_Dispose(body);
CkBinDataW_Dispose(data);
return;
}
wprintf(L"Saved %d attachment bytes.\n",CkBinDataW_getNumBytes(data));
CkJsonObjectW_Dispose(token);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkStringBuilderW_Dispose(messageUrl);
CkStringBuilderW_Dispose(url);
CkStringTableW_Dispose(paths);
CkJsonObjectW_Dispose(part);
CkJsonObjectW_Dispose(selected);
CkStringBuilderW_Dispose(childPath);
CkJsonObjectW_Dispose(body);
CkBinDataW_Dispose(data);
}