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

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  See Unlock Chilkat before making requests.
set http = 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.
http.Login = "SANDBOX_CLIENT_ID"
http.Password = "SANDBOX_CLIENT_SECRET"
http.BasicAuth = 1
set issued = CreateObject("Chilkat.CkDateTime")
success = issued.SetFromCurrentSystemTime()
set resp = CreateObject("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 <> 1) Then
    outFile.WriteLine("No HTTP response: " & http.LastErrorText)
    WScript.Quit
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    set errorInfo = CreateObject("Chilkat.JsonObject")
    success = errorInfo.Load(resp.BodyStr)
    outFile.WriteLine("HTTP " & resp.StatusCode & " " & errorInfo.StringOf("error") & " " & errorInfo.StringOf("error_description"))
    outFile.WriteLine("PayPal debug ID: " & errorInfo.StringOf("debug_id"))
    WScript.Quit
End If

set result = CreateObject("Chilkat.JsonObject")
parsed = result.Load(resp.BodyStr)
If (parsed <> 1) Then
    outFile.WriteLine("Expected a JSON response; HTTP " & resp.StatusCode)
    WScript.Quit
End If

accessToken = result.StringOf("access_token")
lifetime = result.IntOf("expires_in")
If ((result.HasMember("access_token") <> 1) Or (lifetime <= 0)) Then
    outFile.WriteLine("Missing token or lifetime.")
    WScript.Quit
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.
set timing = CreateObject("Chilkat.JsonObject")
success = timing.UpdateString("issued_at",issued.GetAsTimestamp(0))
success = timing.UpdateInt("expires_in",lifetime)
savedTiming = timing.WriteFile("paypal-token-timing.json")
If (savedTiming <> 1) Then
    outFile.WriteLine("Could not save token timing.")
    WScript.Quit
End If

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

outFile.Close