Sample code for 30+ languages & platforms
AutoIt

UPS OAuth2 Client Credentials

See more UPS Examples

Get an OAuth2 access token for the UPS REST API using the client credentials flow (no interactivity with a web browser required).

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.

$oHttp = ObjCreate("Chilkat.Http")

; Implements the following CURL command:

; curl -i -X POST \
;   -u 2498righ8wr6aihe98rt8rhowirtyw9er6twe80rtywrehrt:nerf254578uh8rgt7y3h57358ouyth387h8h53h6yyh80hh578per9y7u5ruyuy4 \
;   https://wwwcie.ups.com/security/v1/oauth/token \
;   -H 'Content-Type: application/x-www-form-urlencoded' \
;   -H 'x-merchant-id: 7B3027' \
;   -d grant_type=client_credentials

; Use the following online tool to generate HTTP code from a CURL command
; Convert a cURL Command to HTTP Source Code

$oHttp.BasicAuth = True
$oHttp.Login = "2498righ8wr6aihe98rt8rhowirtyw9er6twe80rtywrehrt"
$oHttp.Password = "nerf254578uh8rgt7y3h57358ouyth387h8h53h6yyh80hh578per9y7u5ruyuy4"

$oReq = ObjCreate("Chilkat.HttpRequest")
$oReq.AddParam "grant_type","client_credentials"

$oReq.AddHeader "x-merchant-id","7B3027"

$oReq.HttpVerb = "POST"
$oReq.ContentType = "application/x-www-form-urlencoded"

$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpReq("https://wwwcie.ups.com/security/v1/oauth/token",$oReq,$oResp)
If ($bSuccess = False) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

$oSbResponseBody = ObjCreate("Chilkat.StringBuilder")
$oResp.GetBodySb($oSbResponseBody)

$oJResp = ObjCreate("Chilkat.JsonObject")
$oJResp.LoadSb($oSbResponseBody)
$oJResp.EmitCompact = False

ConsoleWrite("Response Body:" & @CRLF)
ConsoleWrite($oJResp.Emit() & @CRLF)

Local $iRespStatusCode = $oResp.StatusCode
ConsoleWrite("Response Status Code = " & $iRespStatusCode & @CRLF)
If ($iRespStatusCode >= 400) Then
    ConsoleWrite("Response Header:" & @CRLF)
    ConsoleWrite($oResp.Header & @CRLF)
    ConsoleWrite("Failed." & @CRLF)
    Exit
EndIf

; Save the OAuth2 access token for other examples to use.
$oJResp.WriteFile("qa_data/tokens/ups_oauth2_token.json")

; If successful, the OAuth2 access token JSON looks like this:

; {
;   "token_type": "Bearer",
;   "issued_at": "1686911985606",
;   "client_id": "2498righ8wr6aihe98rt8rhowirtyw9er6twe80rtywrehrt",
;   "access_token": "eyJraW......R2sbqrY",
;   "expires_in": "14399",
;   "status": "approved"
; }

Local $sToken_type = $oJResp.StringOf("token_type")
Local $sIssued_at = $oJResp.StringOf("issued_at")
Local $sClient_id = $oJResp.StringOf("client_id")
Local $sAccess_token = $oJResp.StringOf("access_token")
Local $sExpires_in = $oJResp.StringOf("expires_in")
Local $status = $oJResp.StringOf("status")