Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Issued
oleobject loo_Resp
oleobject loo_ErrorInfo
oleobject loo_Result
long li_Parsed
string ls_AccessToken
long li_Lifetime
oleobject loo_Timing
long li_SavedTiming

li_Success = 0

//  See Unlock Chilkat before making requests.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
//  Load these values from the credential store on the merchant backoffice workstation.
//  Never embed the merchant secret in an app distributed to customers.
loo_Http.Login = "SANDBOX_CLIENT_ID"
loo_Http.Password = "SANDBOX_CLIENT_SECRET"
loo_Http.BasicAuth = 1
loo_Issued = create oleobject
li_rc = loo_Issued.ConnectToNewObject("Chilkat.CkDateTime")

loo_Issued.SetFromCurrentSystemTime()
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpStr("POST","https://api-m.sandbox.paypal.com/v1/oauth2/token","grant_type=client_credentials","utf-8","application/x-www-form-urlencoded",loo_Resp)
if li_Success <> 1 then
    Write-Debug "No HTTP response: " + loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Issued
    destroy loo_Resp
    return
end if

if (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
    loo_ErrorInfo = create oleobject
    li_rc = loo_ErrorInfo.ConnectToNewObject("Chilkat.JsonObject")

    loo_ErrorInfo.Load(loo_Resp.BodyStr)
    Write-Debug "HTTP " + string(loo_Resp.StatusCode) + " " + loo_ErrorInfo.StringOf("error") + " " + loo_ErrorInfo.StringOf("error_description")
    Write-Debug "PayPal debug ID: " + loo_ErrorInfo.StringOf("debug_id")
    destroy loo_Http
    destroy loo_Issued
    destroy loo_Resp
    destroy loo_ErrorInfo
    return
end if

loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")

li_Parsed = loo_Result.Load(loo_Resp.BodyStr)
if li_Parsed <> 1 then
    Write-Debug "Expected a JSON response; HTTP " + string(loo_Resp.StatusCode)
    destroy loo_Http
    destroy loo_Issued
    destroy loo_Resp
    destroy loo_ErrorInfo
    destroy loo_Result
    return
end if

ls_AccessToken = loo_Result.StringOf("access_token")
li_Lifetime = loo_Result.IntOf("expires_in")
if (loo_Result.HasMember("access_token") <> 1) OR (li_Lifetime <= 0) then
    Write-Debug "Missing token or lifetime."
    destroy loo_Http
    destroy loo_Issued
    destroy loo_Resp
    destroy loo_ErrorInfo
    destroy loo_Result
    return
end if

//  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.
loo_Timing = create oleobject
li_rc = loo_Timing.ConnectToNewObject("Chilkat.JsonObject")

loo_Timing.UpdateString("issued_at",loo_Issued.GetAsTimestamp(0))
loo_Timing.UpdateInt("expires_in",li_Lifetime)
li_SavedTiming = loo_Timing.WriteFile("paypal-token-timing.json")
if li_SavedTiming <> 1 then
    Write-Debug "Could not save token timing."
    destroy loo_Http
    destroy loo_Issued
    destroy loo_Resp
    destroy loo_ErrorInfo
    destroy loo_Result
    destroy loo_Timing
    return
end if

Write-Debug "Token received; lifetime in seconds: " + string(li_Lifetime)


destroy loo_Http
destroy loo_Issued
destroy loo_Resp
destroy loo_ErrorInfo
destroy loo_Result
destroy loo_Timing