Delphi DLL GMail: Get User's GMail Profile

Back to Index

Gets the current user's Gmail profile.

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


var
rest: HCkRest;
success: Boolean;
oauth2: HCkOAuth2;
sbJson: HCkStringBuilder;
json: HCkJsonObject;
emailAddress: PWideChar;
messagesTotal: Integer;
threadsTotal: Integer;
historyId: 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,'userId','matt@chilkat.io');

sbJson := CkStringBuilder_Create();
success := CkRest_FullRequestNoBodySb(rest,'GET','/gmail/v1/users/userId/profile',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.

emailAddress := CkJsonObject__stringOf(json,'emailAddress');
messagesTotal := CkJsonObject_IntOf(json,'messagesTotal');
threadsTotal := CkJsonObject_IntOf(json,'threadsTotal');
historyId := CkJsonObject__stringOf(json,'historyId');

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

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

Sample JSON Response Body

{
  "emailAddress": "matt@chilkat.io",
  "messagesTotal": 3,
  "threadsTotal": 3,
  "historyId": "1257"
}