Tcl Requires Chilkat v11.0.0+
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 0
# See Unlock Chilkat before making requests.
set http [new_CkHttp]
# Load these values from the credential store on the merchant backoffice workstation.
# Never embed the merchant secret in an app distributed to customers.
CkHttp_put_Login $http "SANDBOX_CLIENT_ID"
CkHttp_put_Password $http "SANDBOX_CLIENT_SECRET"
CkHttp_put_BasicAuth $http 1
set issued [new_CkDateTime]
CkDateTime_SetFromCurrentSystemTime $issued
set resp [new_CkHttpResponse]
set 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 != 1} then {
puts "No HTTP response: [CkHttp_lastErrorText $http]"
delete_CkHttp $http
delete_CkDateTime $issued
delete_CkHttpResponse $resp
exit
}
if {expr [[CkHttpResponse_get_StatusCode $resp] < 200] || [[CkHttpResponse_get_StatusCode $resp] >= 300]} then {
set errorInfo [new_CkJsonObject]
CkJsonObject_Load $errorInfo [CkHttpResponse_bodyStr $resp]
puts "HTTP [CkHttpResponse_get_StatusCode $resp] [CkJsonObject_stringOf $errorInfo error] [CkJsonObject_stringOf $errorInfo error_description]"
puts "PayPal debug ID: [CkJsonObject_stringOf $errorInfo debug_id]"
delete_CkHttp $http
delete_CkDateTime $issued
delete_CkHttpResponse $resp
delete_CkJsonObject $errorInfo
exit
}
set result [new_CkJsonObject]
set parsed [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
if {$parsed != 1} then {
puts "Expected a JSON response; HTTP [CkHttpResponse_get_StatusCode $resp]"
delete_CkHttp $http
delete_CkDateTime $issued
delete_CkHttpResponse $resp
delete_CkJsonObject $errorInfo
delete_CkJsonObject $result
exit
}
set accessToken [CkJsonObject_stringOf $result "access_token"]
set lifetime [CkJsonObject_IntOf $result "expires_in"]
if {expr [[CkJsonObject_HasMember $result "access_token"] != 1] || [$lifetime <= 0]} then {
puts "Missing token or lifetime."
delete_CkHttp $http
delete_CkDateTime $issued
delete_CkHttpResponse $resp
delete_CkJsonObject $errorInfo
delete_CkJsonObject $result
exit
}
# 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.
set timing [new_CkJsonObject]
CkJsonObject_UpdateString $timing "issued_at" [CkDateTime_getAsTimestamp $issued 0]
CkJsonObject_UpdateInt $timing "expires_in" $lifetime
set savedTiming [CkJsonObject_WriteFile $timing "paypal-token-timing.json"]
if {$savedTiming != 1} then {
puts "Could not save token timing."
delete_CkHttp $http
delete_CkDateTime $issued
delete_CkHttpResponse $resp
delete_CkJsonObject $errorInfo
delete_CkJsonObject $result
delete_CkJsonObject $timing
exit
}
puts "Token received; lifetime in seconds: $lifetime"
delete_CkHttp $http
delete_CkDateTime $issued
delete_CkHttpResponse $resp
delete_CkJsonObject $errorInfo
delete_CkJsonObject $result
delete_CkJsonObject $timing