AutoIt Requires Chilkat v11.0.0+
AutoIt
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.
Chilkat AutoIt Downloads
Local $bSuccess = False
; See Unlock Chilkat before making requests.
$oHttp = ObjCreate("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.
$oHttp.Login = "SANDBOX_CLIENT_ID"
$oHttp.Password = "SANDBOX_CLIENT_SECRET"
$oHttp.BasicAuth = True
$oIssued = ObjCreate("Chilkat.CkDateTime")
$oIssued.SetFromCurrentSystemTime()
$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpStr("POST","https://api-m.sandbox.paypal.com/v1/oauth2/token","grant_type=client_credentials","utf-8","application/x-www-form-urlencoded",$oResp)
If ($bSuccess <> True) Then
ConsoleWrite("No HTTP response: " & $oHttp.LastErrorText & @CRLF)
Exit
EndIf
If (($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
$oErrorInfo = ObjCreate("Chilkat.JsonObject")
$oErrorInfo.Load($oResp.BodyStr)
ConsoleWrite("HTTP " & $oResp.StatusCode & " " & $oErrorInfo.StringOf("error") & " " & $oErrorInfo.StringOf("error_description") & @CRLF)
ConsoleWrite("PayPal debug ID: " & $oErrorInfo.StringOf("debug_id") & @CRLF)
Exit
EndIf
$oResult = ObjCreate("Chilkat.JsonObject")
Local $bParsed = $oResult.Load($oResp.BodyStr)
If ($bParsed <> True) Then
ConsoleWrite("Expected a JSON response; HTTP " & $oResp.StatusCode & @CRLF)
Exit
EndIf
Local $sAccessToken = $oResult.StringOf("access_token")
Local $iLifetime = $oResult.IntOf("expires_in")
If (($oResult.HasMember("access_token") <> True) Or ($iLifetime <= 0)) Then
ConsoleWrite("Missing token or lifetime." & @CRLF)
Exit
EndIf
; 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.
$oTiming = ObjCreate("Chilkat.JsonObject")
$oTiming.UpdateString("issued_at",$oIssued.GetAsTimestamp(False))
$oTiming.UpdateInt("expires_in",$iLifetime)
Local $bSavedTiming = $oTiming.WriteFile("paypal-token-timing.json")
If ($bSavedTiming <> True) Then
ConsoleWrite("Could not save token timing." & @CRLF)
Exit
EndIf
ConsoleWrite("Token received; lifetime in seconds: " & $iLifetime & @CRLF)