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
(Delphi DLL) BatchModify - Add a Label to each Message in Search ResultsSearchs GMail for messages meeting a criteria and adds a label to each message found.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, StringBuilder, HttpResponse, JsonObject; ... procedure TForm1.Button1Click(Sender: TObject); var success: Boolean; http: HCkHttp; userId: PWideChar; query: PWideChar; url: PWideChar; sb: HCkStringBuilder; json: HCkJsonObject; json2: HCkJsonObject; i: Integer; numMessages: Integer; id: PWideChar; labelId: PWideChar; resp: HCkHttpResponse; begin // 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) then begin Memo1.Lines.Add(CkHttp__lastErrorText(http)); Exit; end; json := CkJsonObject_Create(); CkJsonObject_LoadSb(json,sb); CkJsonObject_putEmitCompact(json,False); Memo1.Lines.Add(CkJsonObject__emit(json)); if (CkHttp_getLastStatus(http) <> 200) then begin Memo1.Lines.Add('Failed.'); Exit; end; // 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) do begin CkJsonObject_putI(json,i); id := CkJsonObject__stringOf(json,'messages[i].id'); CkJsonObject_putI(json2,i); CkJsonObject_UpdateString(json2,'ids[i]',id); i := i + 1; end; // 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); Memo1.Lines.Add(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) then begin Memo1.Lines.Add(CkHttp__lastErrorText(http)); Exit; end; Memo1.Lines.Add('status = ' + IntToStr(CkHttpResponse_getStatusCode(resp))); // A 204 response status indicate success. if (CkHttpResponse_getStatusCode(resp) <> 204) then begin Memo1.Lines.Add(CkHttpResponse__bodyStr(resp)); Memo1.Lines.Add('Failed.'); CkHttpResponse_Dispose(resp); Exit; end; // The 204 response has an empty response body.. CkHttpResponse_Dispose(resp); Memo1.Lines.Add('BatchModify success!'); CkHttp_Dispose(http); CkStringBuilder_Dispose(sb); CkJsonObject_Dispose(json); CkJsonObject_Dispose(json2); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.