Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
Apply a Gmail label to search results in batches
See more GMail REST API Examples
Choose a Gmail search query and an existing label name. Resolve the label ID, collect all matching message IDs before changing any labels, then submit at most 1,000 IDs per batchModify call. Empty searches send no updates. For large mailboxes, spool IDs to persistent storage. Completed batches stay applied if a later request fails; adding the same label again is safe.
Authorize the application with https://www.googleapis.com/auth/gmail.modify. 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;
success = http.HttpNoBody(L"GET",L"https://gmail.googleapis.com/gmail/v1/users/me/labels",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"labels") != 4) {
wprintf(L"Expected a list of labels.\n");
return;
}
CkJsonObjectW label;
success = reply.FindRecord2(L"labels",L"name",L"To review",true,label);
if (success != true) {
wprintf(L"No label with that exact name. Check the name or create the label first.\n");
return;
}
CkStringBuilderW labelId;
success = label.StringOfSb(L"id",labelId);
if (success != true) {
wprintf(L"Could not read the label ID.\n");
return;
}
CkStringTableW ids;
CkStringBuilderW baseUrl;
baseUrl.Append(L"https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=500");
const wchar_t *searchQuery = L"has:attachment";
baseUrl.Append(L"&q=");
baseUrl.Append(http.urlEncode(searchQuery));
CkStringBuilderW url;
CkStringBuilderW pageToken;
bool more = true;
while ((more == true)) {
url.SetString(baseUrl.getAsString());
if (pageToken.get_Length() > 0) {
url.Append(L"&pageToken=");
url.Append(http.urlEncode(pageToken.getAsString()));
}
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;
}
// Gmail can omit messages when the search or mailbox is empty.
int count = reply.SizeOfArray(L"messages");
int i = 0;
while ((i < count)) {
reply.put_I(i);
success = ids.Append(reply.stringOf(L"messages[i].id"));
if (success != true) {
wprintf(L"Could not retain a message ID.\n");
return;
}
i = i + 1;
}
more = false;
if (reply.JsonTypeOf(L"nextPageToken") == 1) {
pageToken.SetString(reply.stringOf(L"nextPageToken"));
more = pageToken.get_Length() > 0;
}
}
// Search is complete. Apply the label to each group and track confirmed progress.
CkJsonObjectW request;
int done = 0;
while ((done < ids.get_Count())) {
request.Clear();
request.UpdateString(L"addLabelIds[0]",labelId.getAsString());
int n = 0;
while (((n < 1000) && ((done + n) < ids.get_Count()))) {
request.put_I(n);
request.UpdateString(L"ids[i]",ids.stringAt(done + n));
n = n + 1;
}
success = http.HttpJson(L"POST",L"https://gmail.googleapis.com/gmail/v1/users/me/messages/batchModify",request,L"application/json",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;
}
done = done + n;
wprintf(L"Messages labeled so far: %d\n",done);
}
wprintf(L"Total messages labeled: %d\n",done);
}