Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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.

PayPal documentation

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oIssued
LOCAL oResp
LOCAL oErrorInfo
LOCAL oResult
LOCAL nParsed
LOCAL cAccessToken
LOCAL nLifetime
LOCAL oTiming
LOCAL nSavedTiming

nSuccess := 0

//  See Unlock Chilkat before making requests.
oHttp := 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.
oHttp:Login := "SANDBOX_CLIENT_ID"
oHttp:Password := "SANDBOX_CLIENT_SECRET"
oHttp:BasicAuth := 1
oIssued := CreateObject("Chilkat.CkDateTime")
oIssued:SetFromCurrentSystemTime()
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpStr("POST", "https://api-m.sandbox.paypal.com/v1/oauth2/token", "grant_type=client_credentials", "utf-8", "application/x-www-form-urlencoded", oResp)
IF (nSuccess != 1)
    ? "No HTTP response: " + oHttp:LastErrorText
    oHttp:destroy()
    oIssued:destroy()
    oResp:destroy()
    RETURN
ENDIF

IF ((oResp:StatusCode < 200) .OR. (oResp:StatusCode >= 300))
    oErrorInfo := CreateObject("Chilkat.JsonObject")
    oErrorInfo:Load(oResp:BodyStr)
    ? "HTTP " + Str(oResp:StatusCode) + " " + oErrorInfo:StringOf("error") + " " + oErrorInfo:StringOf("error_description")
    ? "PayPal debug ID: " + oErrorInfo:StringOf("debug_id")
    oHttp:destroy()
    oIssued:destroy()
    oResp:destroy()
    oErrorInfo:destroy()
    RETURN
ENDIF

oResult := CreateObject("Chilkat.JsonObject")
nParsed := oResult:Load(oResp:BodyStr)
IF (nParsed != 1)
    ? "Expected a JSON response; HTTP " + Str(oResp:StatusCode)
    oHttp:destroy()
    oIssued:destroy()
    oResp:destroy()
    oErrorInfo:destroy()
    oResult:destroy()
    RETURN
ENDIF

cAccessToken := oResult:StringOf("access_token")
nLifetime := oResult:IntOf("expires_in")
IF ((oResult:HasMember("access_token") != 1) .OR. (nLifetime <= 0))
    ? "Missing token or lifetime."
    oHttp:destroy()
    oIssued:destroy()
    oResp:destroy()
    oErrorInfo:destroy()
    oResult:destroy()
    RETURN
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.
oTiming := CreateObject("Chilkat.JsonObject")
oTiming:UpdateString("issued_at", oIssued:GetAsTimestamp(0))
oTiming:UpdateInt("expires_in", nLifetime)
nSavedTiming := oTiming:WriteFile("paypal-token-timing.json")
IF (nSavedTiming != 1)
    ? "Could not save token timing."
    oHttp:destroy()
    oIssued:destroy()
    oResp:destroy()
    oErrorInfo:destroy()
    oResult:destroy()
    oTiming:destroy()
    RETURN
ENDIF

? "Token received; lifetime in seconds: " + Str(nLifetime)

oHttp:destroy()
oIssued:destroy()
oResp:destroy()
oErrorInfo:destroy()
oResult:destroy()
oTiming:destroy()