Delphi DLL
Delphi DLL
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 Delphi DLL Downloads
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
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) 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 := CkHttpResponse_Create();
success := CkHttp_HttpJson(http,'POST',url,json2,'application/json',resp);
if (success = False) 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.');
Exit;
end;
// The 204 response has an empty response body..
Memo1.Lines.Add('BatchModify success!');
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sb);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(json2);
CkHttpResponse_Dispose(resp);
end;