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

Visual Basic 6.0
Dim success As Long
success = 0

'  See Unlock Chilkat before making requests.
Dim http As New ChilkatHttp
'  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 = 1
Dim issued As New CkDateTime
success = issued.SetFromCurrentSystemTime()
Dim resp As New ChilkatHttpResponse
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 <> 1) Then
    Debug.Print "No HTTP response: " & http.LastErrorText
    Exit Sub
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    Dim errorInfo As New ChilkatJsonObject
    success = errorInfo.Load(resp.BodyStr)
    Debug.Print "HTTP " & resp.StatusCode & " " & errorInfo.StringOf("error") & " " & errorInfo.StringOf("error_description")
    Debug.Print "PayPal debug ID: " & errorInfo.StringOf("debug_id")
    Exit Sub
End If

Dim result As New ChilkatJsonObject
Dim parsed As Long
parsed = result.Load(resp.BodyStr)
If (parsed <> 1) Then
    Debug.Print "Expected a JSON response; HTTP " & resp.StatusCode
    Exit Sub
End If

Dim accessToken As String
accessToken = result.StringOf("access_token")
Dim lifetime As Long
lifetime = result.IntOf("expires_in")
If ((result.HasMember("access_token") <> 1) Or (lifetime <= 0)) Then
    Debug.Print "Missing token or lifetime."
    Exit Sub
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 ChilkatJsonObject
success = timing.UpdateString("issued_at",issued.GetAsTimestamp(0))
success = timing.UpdateInt("expires_in",lifetime)
Dim savedTiming As Long
savedTiming = timing.WriteFile("paypal-token-timing.json")
If (savedTiming <> 1) Then
    Debug.Print "Could not save token timing."
    Exit Sub
End If

Debug.Print "Token received; lifetime in seconds: " & lifetime