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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  See Unlock Chilkat before making requests.
set http = Server.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 = Server.CreateObject("Chilkat.CkDateTime")
success = issued.SetFromCurrentSystemTime()
set resp = Server.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
    Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response: " & http.LastErrorText) & "</pre>"
    Response.End
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    set errorInfo = Server.CreateObject("Chilkat.JsonObject")
    success = errorInfo.Load(resp.BodyStr)
    Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & " " & errorInfo.StringOf("error") & " " & errorInfo.StringOf("error_description")) & "</pre>"
    Response.Write "<pre>" & Server.HTMLEncode( "PayPal debug ID: " & errorInfo.StringOf("debug_id")) & "</pre>"
    Response.End
End If

set result = Server.CreateObject("Chilkat.JsonObject")
parsed = result.Load(resp.BodyStr)
If (parsed <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected a JSON response; HTTP " & resp.StatusCode) & "</pre>"
    Response.End
End If

accessToken = result.StringOf("access_token")
lifetime = result.IntOf("expires_in")
If ((result.HasMember("access_token") <> 1) Or (lifetime <= 0)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing token or lifetime.") & "</pre>"
    Response.End
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 = Server.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
    Response.Write "<pre>" & Server.HTMLEncode( "Could not save token timing.") & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Token received; lifetime in seconds: " & lifetime) & "</pre>"

%>
</body>
</html>