Delphi DLL GMail: Get a specific message (format="minimal")

Back to Index

Gets a specific message using format=minimal.

Documentation: https://developers.google.com/gmail/api/v1/reference/users/messages/get


var
rest: HCkRest;
success: Boolean;
oauth2: HCkOAuth2;
sbJson: HCkStringBuilder;
json: HCkJsonObject;
id: PWideChar;
threadId: PWideChar;
snippet: PWideChar;
historyId: PWideChar;
internalDate: PWideChar;
sizeEstimate: Integer;
i: Integer;
count_i: Integer;
strVal: PWideChar;

begin
rest := CkRest_Create();

//   Provide a previously obtained OAuth2 access token.
oauth2 := CkOAuth2_Create();
CkOAuth2_putAccessToken(oauth2,'OAUTH2_ACCESS_TOKEN');
CkRest_SetAuthOAuth2(rest,oauth2);

success := CkRest_Connect(rest,'www.googleapis.com',443,True,True);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

CkRest_AddPathParam(rest,'messageId','15fc237e79da4174');

CkRest_AddQueryParam(rest,'format','minimal');

sbJson := CkStringBuilder_Create();
success := CkRest_FullRequestNoBodySb(rest,'GET','/gmail/v1/users/me/messages/messageId',sbJson);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

if (CkRest_getResponseStatusCode(rest) <> 200) then
  begin
    Memo1.Lines.Add('Received error response code: ' + IntToStr(CkRest_getResponseStatusCode(rest)));
    Memo1.Lines.Add('Response body:');
    Memo1.Lines.Add(CkStringBuilder__getAsString(sbJson));
    Exit;
  end;

json := CkJsonObject_Create();
CkJsonObject_LoadSb(json,sbJson);

//  The following code parses the JSON response.
//  A sample JSON response is shown below the sample code.

id := CkJsonObject__stringOf(json,'id');
threadId := CkJsonObject__stringOf(json,'threadId');
snippet := CkJsonObject__stringOf(json,'snippet');
historyId := CkJsonObject__stringOf(json,'historyId');
internalDate := CkJsonObject__stringOf(json,'internalDate');
sizeEstimate := CkJsonObject_IntOf(json,'sizeEstimate');
i := 0;
count_i := CkJsonObject_SizeOfArray(json,'labelIds');
while i < count_i do
  begin
CkJsonObject_putI(json,i);
    strVal := CkJsonObject__stringOf(json,'labelIds[i]');
    i := i + 1;
  end;

Memo1.Lines.Add('Example Completed.');

CkRest_Dispose(rest);
CkOAuth2_Dispose(oauth2);
CkStringBuilder_Dispose(sbJson);
CkJsonObject_Dispose(json);

Sample JSON Response Body

{
  "id": "15fc237e79da4174",
  "threadId": "15fc237e79da4174",
  "labelIds": [
    "IMPORTANT",
    "SENT",
    "INBOX"
  ],
  "snippet": "This is a test email with 2 attachments.",
  "historyId": "1582",
  "internalDate": "1510791964000",
  "sizeEstimate": 11153
}