Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
PayPal: Get a sandbox access token
See more PayPal Examples
Exchange a sandbox REST app client ID and secret for a bearer token on a merchant-controlled backoffice workstation. PayPal supplies its lifetime in seconds. Store the token in a protected credential store; the companion expiry example reads only timing metadata.
Chilkat Delphi DLL Downloads
var
success: Boolean;
http: HCkHttp;
issued: HCkDateTime;
resp: HCkHttpResponse;
errorInfo: HCkJsonObject;
result: HCkJsonObject;
parsed: Boolean;
accessToken: PWideChar;
lifetime: Integer;
timing: HCkJsonObject;
savedTiming: Boolean;
begin
success := False;
// See Unlock Chilkat before making requests.
http := CkHttp_Create();
// Load these values from the credential store on the merchant backoffice workstation.
// Never embed the merchant secret in an app distributed to customers.
CkHttp_putLogin(http,'SANDBOX_CLIENT_ID');
CkHttp_putPassword(http,'SANDBOX_CLIENT_SECRET');
CkHttp_putBasicAuth(http,True);
issued := CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(issued);
resp := CkHttpResponse_Create();
success := CkHttp_HttpStr(http,'POST','https://api-m.sandbox.paypal.com/v1/oauth2/token','grant_type=client_credentials','utf-8','application/x-www-form-urlencoded',resp);
if (success <> True) then
begin
Memo1.Lines.Add('No HTTP response: ' + CkHttp__lastErrorText(http));
Exit;
end;
if ((CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
begin
errorInfo := CkJsonObject_Create();
CkJsonObject_Load(errorInfo,CkHttpResponse__bodyStr(resp));
Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkJsonObject__stringOf(errorInfo,'error') + ' ' + CkJsonObject__stringOf(errorInfo,'error_description'));
Memo1.Lines.Add('PayPal debug ID: ' + CkJsonObject__stringOf(errorInfo,'debug_id'));
Exit;
end;
result := CkJsonObject_Create();
parsed := CkJsonObject_Load(result,CkHttpResponse__bodyStr(resp));
if (parsed <> True) then
begin
Memo1.Lines.Add('Expected a JSON response; HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
Exit;
end;
accessToken := CkJsonObject__stringOf(result,'access_token');
lifetime := CkJsonObject_IntOf(result,'expires_in');
if ((CkJsonObject_HasMember(result,'access_token') <> True) or (lifetime <= 0)) then
begin
Memo1.Lines.Add('Missing token or lifetime.');
Exit;
end;
// Supply accessToken to Http.AuthToken. Never log the bearer token or client secret.
// Save only timing metadata here, relative to the start of the token request.
timing := CkJsonObject_Create();
CkJsonObject_UpdateString(timing,'issued_at',CkDateTime__getAsTimestamp(issued,False));
CkJsonObject_UpdateInt(timing,'expires_in',lifetime);
savedTiming := CkJsonObject_WriteFile(timing,'paypal-token-timing.json');
if (savedTiming <> True) then
begin
Memo1.Lines.Add('Could not save token timing.');
Exit;
end;
Memo1.Lines.Add('Token received; lifetime in seconds: ' + IntToStr(lifetime));
CkHttp_Dispose(http);
CkDateTime_Dispose(issued);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(errorInfo);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(timing);