Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
Mark a Gmail message as read or archive it
See more GMail REST API Examples
Choose read or archive and supply a Gmail message ID. Marking a message as read removes UNREAD. Archiving removes INBOX while keeping the message available elsewhere in the mailbox. Each run performs the selected action on one message. Removing a label that is already absent is safe to repeat.
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>
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;
const wchar_t *action = L"read";
CkJsonObjectW request;
if (action == L"read") {
success = request.UpdateString(L"removeLabelIds[0]",L"UNREAD");
if (success != true) {
wprintf(L"%s\n",request.lastErrorText());
return;
}
}
else {
if (action != L"archive") {
wprintf(L"Choose read or archive.\n");
return;
}
success = request.UpdateString(L"removeLabelIds[0]",L"INBOX");
if (success != true) {
wprintf(L"%s\n",request.lastErrorText());
return;
}
}
CkStringBuilderW url;
url.Append(L"https://gmail.googleapis.com/gmail/v1/users/me/messages/");
url.Append(L"GMAIL_MESSAGE_ID");
url.Append(L"/modify");
success = http.HttpJson(L"POST",url.getAsString(),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;
}
success = reply.Load(resp.bodyStr());
if (success != true) {
wprintf(L"Expected a Gmail JSON response.\n");
return;
}
if ((reply.JsonTypeOf(L"id") != 1) || (reply.StringOfEquals(L"id",L"",true) == true)) {
wprintf(L"Expected id in the response.\n");
return;
}
wprintf(L"Completed %s for message %s\n",action,reply.stringOf(L"id"));
}