Visual FoxPro Requires Chilkat v11.0.0+
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loHttp
LOCAL loIssued
LOCAL loResp
LOCAL loErrorInfo
LOCAL loResult
LOCAL lnParsed
LOCAL lcAccessToken
LOCAL lnLifetime
LOCAL loTiming
LOCAL lnSavedTiming
lnSuccess = 0
* See Unlock Chilkat before making requests.
loHttp = CreateObject('Chilkat.Http')
* Load these values from the credential store on the merchant backoffice workstation.
* Never embed the merchant secret in an app distributed to customers.
loHttp.Login = "SANDBOX_CLIENT_ID"
loHttp.Password = "SANDBOX_CLIENT_SECRET"
loHttp.BasicAuth = 1
loIssued = CreateObject('Chilkat.CkDateTime')
loIssued.SetFromCurrentSystemTime()
loResp = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpStr("POST","https://api-m.sandbox.paypal.com/v1/oauth2/token","grant_type=client_credentials","utf-8","application/x-www-form-urlencoded",loResp)
IF (lnSuccess <> 1) THEN
? "No HTTP response: " + loHttp.LastErrorText
RELEASE loHttp
RELEASE loIssued
RELEASE loResp
CANCEL
ENDIF
IF ((loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
loErrorInfo = CreateObject('Chilkat.JsonObject')
loErrorInfo.Load(loResp.BodyStr)
? "HTTP " + STR(loResp.StatusCode) + " " + loErrorInfo.StringOf("error") + " " + loErrorInfo.StringOf("error_description")
? "PayPal debug ID: " + loErrorInfo.StringOf("debug_id")
RELEASE loHttp
RELEASE loIssued
RELEASE loResp
RELEASE loErrorInfo
CANCEL
ENDIF
loResult = CreateObject('Chilkat.JsonObject')
lnParsed = loResult.Load(loResp.BodyStr)
IF (lnParsed <> 1) THEN
? "Expected a JSON response; HTTP " + STR(loResp.StatusCode)
RELEASE loHttp
RELEASE loIssued
RELEASE loResp
RELEASE loErrorInfo
RELEASE loResult
CANCEL
ENDIF
lcAccessToken = loResult.StringOf("access_token")
lnLifetime = loResult.IntOf("expires_in")
IF ((loResult.HasMember("access_token") <> 1) OR (lnLifetime <= 0)) THEN
? "Missing token or lifetime."
RELEASE loHttp
RELEASE loIssued
RELEASE loResp
RELEASE loErrorInfo
RELEASE loResult
CANCEL
ENDIF
* 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.
loTiming = CreateObject('Chilkat.JsonObject')
loTiming.UpdateString("issued_at",loIssued.GetAsTimestamp(0))
loTiming.UpdateInt("expires_in",lnLifetime)
lnSavedTiming = loTiming.WriteFile("paypal-token-timing.json")
IF (lnSavedTiming <> 1) THEN
? "Could not save token timing."
RELEASE loHttp
RELEASE loIssued
RELEASE loResp
RELEASE loErrorInfo
RELEASE loResult
RELEASE loTiming
CANCEL
ENDIF
? "Token received; lifetime in seconds: " + STR(lnLifetime)
RELEASE loHttp
RELEASE loIssued
RELEASE loResp
RELEASE loErrorInfo
RELEASE loResult
RELEASE loTiming