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

VB.NET
Dim success As Boolean = 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
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
    Debug.WriteLine("No HTTP response: " & http.LastErrorText)
    Exit Sub
End If

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

Dim result As New Chilkat.JsonObject
Dim parsed As Boolean = result.Load(resp.BodyStr)
If (parsed <> True) Then
    Debug.WriteLine("Expected a JSON response; HTTP " & resp.StatusCode)
    Exit Sub
End If

Dim accessToken As String = result.StringOf("access_token")
Dim lifetime As Integer = result.IntOf("expires_in")
If ((result.HasMember("access_token") <> True) Or (lifetime <= 0)) Then
    Debug.WriteLine("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 Chilkat.JsonObject
timing.UpdateString("issued_at",issued.GetAsTimestamp(False))
timing.UpdateInt("expires_in",lifetime)
Dim savedTiming As Boolean = timing.WriteFile("paypal-token-timing.json")
If (savedTiming <> True) Then
    Debug.WriteLine("Could not save token timing.")
    Exit Sub
End If

Debug.WriteLine("Token received; lifetime in seconds: " & lifetime)