Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(C) BatchModify - Add a Label to each Message in Search ResultsSearchs GMail for messages meeting a criteria and adds a label to each message found.
#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; // 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 != TRUE) { 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 = CkHttp_PostJson3(http,url,"application/json",json2); if (CkHttp_getLastMethodSuccess(http) != TRUE) { printf("%s\n",CkHttp_lastErrorText(http)); CkHttp_Dispose(http); CkStringBuilder_Dispose(sb); CkJsonObject_Dispose(json); CkJsonObject_Dispose(json2); 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"); CkHttpResponse_Dispose(resp); CkHttp_Dispose(http); CkStringBuilder_Dispose(sb); CkJsonObject_Dispose(json); CkJsonObject_Dispose(json2); return; } // The 204 response has an empty response body.. CkHttpResponse_Dispose(resp); printf("BatchModify success!\n"); CkHttp_Dispose(http); CkStringBuilder_Dispose(sb); CkJsonObject_Dispose(json); CkJsonObject_Dispose(json2); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.