Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Test Salesforce OAuth2 Access Token
See more Salesforce Examples
Demonstrates how to make a simple Salesforce REST API call to test a previously obtained access token.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;
http: THttp;
json: TJsonObject;
sbUrl: TStringBuilder;
sbResponseBody: TStringBuilder;
jsonResponse: TJsonObject;
begin
success := False;
// This example does the following:
// curl -X GET https://yourInstance.salesforce.com/services/oauth2/userinfo \
// -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
http := THttp.Create;
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// This example assumes the OAuth2 access token was previously fetched
// and saved to a file. See Get SalesForce OAuth2 Access Token via Authorization Flow
json := TJsonObject.Create;
success := json.LoadFile('qa_data/tokens/_salesforce.json');
if (success = False) then
begin
WriteLn('Failed to load OAuth2 access token.');
Exit;
end;
// Here's an example of the JSON:
// {
// "access_token": "00D41000....uLZBpT6",
// "refresh_token": "5Aep....25xdGgkrV",
// "signature": "cjTbSc5DvcKpaMoRTzuQTJLb1tcMw8LEO01flq4aMD4=",
// "scope": "refresh_token id",
// "instance_url": "https://d41000000f8a0eak-dev-ed.my.salesforce.com",
// "id": "https://login.salesforce.com/id/00D41000000F8A0EAK/005410000....xAAE",
// "token_type": "Bearer",
// "issued_at": "1738348388166"
// }
// Adds the "Authorization: Bearer YOUR_ACCESS_TOKEN" header.
http.AuthToken := json.StringOf('access_token');
// We want to build the following URL: https://<instance_id>.salesforce.com/services/oauth2/userinfo
sbUrl := TStringBuilder.Create;
sbUrl.Append(json.StringOf('instance_url'));
sbUrl.Append('/services/oauth2/userinfo');
sbResponseBody := TStringBuilder.Create;
success := http.QuickGetSb(sbUrl.GetAsString(),sbResponseBody);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
WriteLn('Response status code = ' + http.LastStatus);
jsonResponse := TJsonObject.Create;
jsonResponse.LoadSb(sbResponseBody);
jsonResponse.EmitCompact := False;
WriteLn(jsonResponse.Emit());
// The expected JSON response is something like this:
// {
// "sub": "005xxxxxxxxxxxx",
// "name": "John Doe",
// "preferred_username": "johndoe@example.com",
// "email": "johndoe@example.com",
// "profile": "https://na85.salesforce.com/005xxxxxxxxxxxx"
// ...
// ...
// }
http.Free;
json.Free;
sbUrl.Free;
sbResponseBody.Free;
jsonResponse.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.