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

Xojo Plugin
Dim success As Boolean
success = False

//  See Unlock Chilkat before making requests.
Dim http As New 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.
http.Login = "SANDBOX_CLIENT_ID"
http.Password = "SANDBOX_CLIENT_SECRET"
http.BasicAuth = True
Dim issued As New Chilkat.CkDateTime
success = issued.SetFromCurrentSystemTime()
Dim resp As New Chilkat.HttpResponse
success = http.HttpStr("POST","https://api-m.sandbox.paypal.com/v1/oauth2/token","grant_type=client_credentials","utf-8","application/x-www-form-urlencoded",resp)
If (success <> True) Then
    System.DebugLog("No HTTP response: " + http.LastErrorText)
    Return
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    Dim errorInfo As New Chilkat.JsonObject
    success = errorInfo.Load(resp.BodyStr)
    System.DebugLog("HTTP " + Str(resp.StatusCode) + " " + errorInfo.StringOf("error") + " " + errorInfo.StringOf("error_description"))
    System.DebugLog("PayPal debug ID: " + errorInfo.StringOf("debug_id"))
    Return
End If

Dim result As New Chilkat.JsonObject
Dim parsed As Boolean
parsed = result.Load(resp.BodyStr)
If (parsed <> True) Then
    System.DebugLog("Expected a JSON response; HTTP " + Str(resp.StatusCode))
    Return
End If

Dim accessToken As String
accessToken = result.StringOf("access_token")
Dim lifetime As Int32
lifetime = result.IntOf("expires_in")
If ((result.HasMember("access_token") <> True) Or (lifetime <= 0)) Then
    System.DebugLog("Missing token or lifetime.")
    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.
Dim timing As New Chilkat.JsonObject
success = timing.UpdateString("issued_at",issued.GetAsTimestamp(False))
success = timing.UpdateInt("expires_in",lifetime)
Dim savedTiming As Boolean
savedTiming = timing.WriteFile("paypal-token-timing.json")
If (savedTiming <> True) Then
    System.DebugLog("Could not save token timing.")
    Return
End If

System.DebugLog("Token received; lifetime in seconds: " + Str(lifetime))