C
C
BatchModify - Add a Label to each Message in Search Results
See more GMail REST API Examples
Searchs GMail for messages meeting a criteria and adds a label to each message found.Chilkat C Downloads
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
const char *userId;
const char *query;
const char *url;
HCkStringBuilder sb;
HCkJsonObject json;
HCkJsonObject json2;
int i;
int numMessages;
const char *id;
const char *labelId;
HCkHttpResponse resp;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttp_Create();
CkHttp_putAuthToken(http,"GMAIL-ACCESS-TOKEN");
userId = "me";
CkHttp_SetUrlVar(http,"userId",userId);
query = "subject:questions";
CkHttp_SetUrlVar(http,"query",query);
url = "https://www.googleapis.com/gmail/v1/users/{$userId}/messages?q={$query}";
sb = CkStringBuilder_Create();
success = CkHttp_QuickGetSb(http,url,sb);
if (success == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sb);
return;
}
json = CkJsonObject_Create();
CkJsonObject_LoadSb(json,sb);
CkJsonObject_putEmitCompact(json,FALSE);
printf("%s\n",CkJsonObject_emit(json));
if (CkHttp_getLastStatus(http) != 200) {
printf("Failed.\n");
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sb);
CkJsonObject_Dispose(json);
return;
}
// If successful, the received JSON looks like this:
// {
// "messages": [
// {
// "id": "166f583051d36144",
// "threadId": "166f583051d36144"
// },
// {
// "id": "166f5815e1f36144",
// "threadId": "166f5815e1f36144"
// },
// ...
// {
// "id": "166f580639e36144",
// "threadId": "166f580639e36144"
// },
// {
// "id": "15dbc2e28ec789c6",
// "threadId": "15dbc2e28ec789c6"
// }
// ],
// "nextPageToken": "13434766102274844688",
// "resultSizeEstimate": 103
// }
//
// Next, we'll be sending an HTTP POST to add the label "questions" to each message in the
// search results. The JSON to be sent for the batchModify is this:
// {
// "ids": [
// string
// ],
// "addLabelIds": [
// string
// ],
// "removeLabelIds": [
// string
// ]
// }
// We'll omit "removeLabelIds" because we're not removing any labels.
// We are parsing the JSON search results, and at the same time building the batchModify JSON.
json2 = CkJsonObject_Create();
i = 0;
numMessages = CkJsonObject_SizeOfArray(json,"messages");
while ((i < numMessages)) {
CkJsonObject_putI(json,i);
id = CkJsonObject_stringOf(json,"messages[i].id");
CkJsonObject_putI(json2,i);
CkJsonObject_UpdateString(json2,"ids[i]",id);
i = i + 1;
}
// We need the id of the label (not the name).
// I know the name of the label is "questions", but I need to know the id.
// See this example: Get Label Id by Name
// The id of my label named "questions" is "Label_43"
labelId = "Label_43";
CkJsonObject_UpdateString(json2,"addLabelIds[0]",labelId);
CkJsonObject_UpdateNewArray(json2,"removeLabelIds");
CkJsonObject_putEmitCompact(json2,FALSE);
printf("%s\n",CkJsonObject_emit(json2));
// Send the batchModify
url = "https://www.googleapis.com/gmail/v1/users/{$userId}/messages/batchModify";
resp = CkHttpResponse_Create();
success = CkHttp_HttpJson(http,"POST",url,json2,"application/json",resp);
if (success == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sb);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(json2);
CkHttpResponse_Dispose(resp);
return;
}
printf("status = %d\n",CkHttpResponse_getStatusCode(resp));
// A 204 response status indicate success.
if (CkHttpResponse_getStatusCode(resp) != 204) {
printf("%s\n",CkHttpResponse_bodyStr(resp));
printf("Failed.\n");
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sb);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(json2);
CkHttpResponse_Dispose(resp);
return;
}
// The 204 response has an empty response body..
printf("BatchModify success!\n");
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sb);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(json2);
CkHttpResponse_Dispose(resp);
}