DataFlex Requires Chilkat v11.0.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Handle hoIssued
Variant vResp
Handle hoResp
Handle hoErrorInfo
Handle hoResult
Boolean iParsed
String sAccessToken
Integer iLifetime
Handle hoTiming
Boolean iSavedTiming
String sTemp1
String sTemp2
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
// See Unlock Chilkat before making requests.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
// Load these values from the credential store on the merchant backoffice workstation.
// Never embed the merchant secret in an app distributed to customers.
Set ComLogin Of hoHttp To "SANDBOX_CLIENT_ID"
Set ComPassword Of hoHttp To "SANDBOX_CLIENT_SECRET"
Set ComBasicAuth Of hoHttp To True
Get Create (RefClass(cComCkDateTime)) To hoIssued
If (Not(IsComObjectCreated(hoIssued))) Begin
Send CreateComObject of hoIssued
End
Get ComSetFromCurrentSystemTime Of hoIssued To iSuccess
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get pvComObject of hoResp to vResp
Get ComHttpStr Of hoHttp "POST" "https://api-m.sandbox.paypal.com/v1/oauth2/token" "grant_type=client_credentials" "utf-8" "application/x-www-form-urlencoded" vResp To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln "No HTTP response: " sTemp1
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get Create (RefClass(cComChilkatJsonObject)) To hoErrorInfo
If (Not(IsComObjectCreated(hoErrorInfo))) Begin
Send CreateComObject of hoErrorInfo
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoErrorInfo sTemp1 To iSuccess
Get ComStatusCode Of hoResp To iTemp1
Get ComStringOf Of hoErrorInfo "error" To sTemp1
Get ComStringOf Of hoErrorInfo "error_description" To sTemp2
Showln "HTTP " iTemp1 " " sTemp1 " " sTemp2
Get ComStringOf Of hoErrorInfo "debug_id" To sTemp1
Showln "PayPal debug ID: " sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoResult sTemp1 To iParsed
If (iParsed <> True) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Expected a JSON response; HTTP " iTemp1
Procedure_Return
End
Get ComStringOf Of hoResult "access_token" To sAccessToken
Get ComIntOf Of hoResult "expires_in" To iLifetime
Get ComHasMember Of hoResult "access_token" To bTemp1
If ((bTemp1 <> True) Or (iLifetime <= 0)) Begin
Showln "Missing token or lifetime."
Procedure_Return
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.
Get Create (RefClass(cComChilkatJsonObject)) To hoTiming
If (Not(IsComObjectCreated(hoTiming))) Begin
Send CreateComObject of hoTiming
End
Get ComGetAsTimestamp Of hoIssued False To sTemp1
Get ComUpdateString Of hoTiming "issued_at" sTemp1 To iSuccess
Get ComUpdateInt Of hoTiming "expires_in" iLifetime To iSuccess
Get ComWriteFile Of hoTiming "paypal-token-timing.json" To iSavedTiming
If (iSavedTiming <> True) Begin
Showln "Could not save token timing."
Procedure_Return
End
Showln "Token received; lifetime in seconds: " iLifetime
End_Procedure