Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
Inspect the MIME parts of a Gmail message
See more GMail REST API Examples
Supply a Gmail message ID and request format=full. Walk the root payload and every nested parts array, including a single-part root. For each part, show its MIME type, filename, and size. A body.attachmentId can be passed to the attachment download example. Inline body.data is already included as base64url.
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 <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>
#include <CkStringTableW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkJsonObjectW token;
success = token.LoadFile(L"gmail-token.json");
if (success != true) {
wprintf(L"Could not read gmail-token.json from the private working directory.\n");
return;
}
if ((token.JsonTypeOf(L"access_token") != 1) || (token.StringOfEquals(L"access_token",L"",true) == true)) {
wprintf(L"An OAuth access token is required.\n");
return;
}
CkHttpW http;
http.put_RequireSslCertVerify(true);
http.put_AuthToken(token.stringOf(L"access_token"));
CkHttpResponseW resp;
CkJsonObjectW reply;
CkStringBuilderW url;
url.Append(L"https://gmail.googleapis.com/gmail/v1/users/me/messages/");
url.Append(L"GMAIL_MESSAGE_ID");
url.Append(L"?format=full");
success = http.HttpNoBody(L"GET",url.getAsString(),resp);
if (resp.get_StatusCode() == 0) {
wprintf(L"Gmail did not return an HTTP response.\n");
return;
}
if ((success != true) || (resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
wprintf(L"Gmail HTTP %d\n",resp.get_StatusCode());
success = reply.Load(resp.bodyStr());
if (success == true) {
wprintf(L"%s\n",reply.stringOf(L"error.message"));
}
return;
}
success = reply.Load(resp.bodyStr());
if (success != true) {
wprintf(L"Expected a Gmail JSON response.\n");
return;
}
if (reply.JsonTypeOf(L"payload") != 3) {
wprintf(L"Expected a MIME payload.\n");
return;
}
// Queue paths into the original JSON document so any MIME nesting depth is supported.
CkStringTableW paths;
success = paths.Append(L"payload");
if (success != true) {
wprintf(L"Could not queue the root part.\n");
return;
}
CkJsonObjectW part;
CkStringBuilderW childPath;
int p = 0;
while ((p < paths.get_Count())) {
success = reply.ObjectOf2(paths.stringAt(p),part);
if (success != true) {
wprintf(L"Could not read a MIME part.\n");
return;
}
wprintf(L"Part: %s bytes: %d\n",part.stringOf(L"mimeType"),part.IntOf(L"body.size"));
if (part.JsonTypeOf(L"filename") == 1) {
if (part.StringOfEquals(L"filename",L"",true) != true) {
wprintf(L"Filename: %s\n",part.stringOf(L"filename"));
}
}
if (part.JsonTypeOf(L"body.attachmentId") == 1) {
wprintf(L"Attachment ID: %s\n",part.stringOf(L"body.attachmentId"));
}
else {
if (part.JsonTypeOf(L"body.data") == 1) {
wprintf(L"Body data is available in this part.\n");
}
}
int j = 0;
while ((j < part.SizeOfArray(L"parts"))) {
childPath.SetString(paths.stringAt(p));
childPath.Append(L".parts[");
childPath.AppendInt(j);
childPath.Append(L"]");
success = paths.Append(childPath.getAsString());
if (success != true) {
wprintf(L"Could not queue a child part.\n");
return;
}
j = j + 1;
}
p = p + 1;
}
}