Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Google OAuth2 v3 userinfo (https://www.googleapis.com/oauth2/v3/userinfo)
See more Google APIs Examples
Demonstrates how to get the Google OAuth2 v3 userinfo.Important: Make sure the OAuth2 access token included "profile" in the list of scopes.
Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.StringBuilder,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
jsonToken: TJsonObject;
http: THttp;
sbResponseBody: TStringBuilder;
json: TJsonObject;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// It is assumed we previously obtained an OAuth2 access token.
// This example loads the JSON access token file
// originally obtained by this example: Get Google People API OAuth2 Access Token
// or refreshed by this example: Refresh Google People API OAuth2 Access Token
jsonToken := TJsonObject.Create;
success := jsonToken.LoadFile('qa_data/tokens/googlePeople.json');
if (success <> True) then
begin
WriteLn('Failed to load googleContacts.json');
Exit;
end;
http := THttp.Create;
// -------------------------------------------------------------------------------------
// Make sure the access token was obtained with "profile" included in the list of scopes.
// -------------------------------------------------------------------------------------
http.AuthToken := jsonToken.StringOf('access_token');
sbResponseBody := TStringBuilder.Create;
success := http.QuickGetSb('https://www.googleapis.com/oauth2/v3/userinfo',sbResponseBody);
if (success = False) then
begin
WriteLn(http.LastErrorText);
WriteLn(sbResponseBody.GetAsString());
Exit;
end;
// Sample JSON Response
json := TJsonObject.Create;
json.EmitCompact := False;
json.Load(sbResponseBody.GetAsString());
WriteLn(json.Emit());
// Sample response:
// {
// "sub": "119999690625687999964",
// "name": "Matt",
// "given_name": "Matt",
// "picture": "https://lh3.googleusercontent.com/-b4...eg3Zjo/AAAAAAAAAAI/AAAAAAAAAAA/AMZuu....3RpdA/s96-c/photo.jpg",
// "locale": "en"
// }
//
jsonToken.Free;
http.Free;
sbResponseBody.Free;
json.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.