Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, HttpRequest, HttpResponse, StringBuilder, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
http: HCkHttp;
req: HCkHttpRequest;
resp: HCkHttpResponse;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
token_type: PWideChar;
issued_at: PWideChar;
client_id: PWideChar;
access_token: PWideChar;
expires_in: PWideChar;
status: PWideChar;

begin
success := False;

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

http := CkHttp_Create();

// 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

CkHttp_putBasicAuth(http,True);
CkHttp_putLogin(http,'2498righ8wr6aihe98rt8rhowirtyw9er6twe80rtywrehrt');
CkHttp_putPassword(http,'nerf254578uh8rgt7y3h57358ouyth387h8h53h6yyh80hh578per9y7u5ruyuy4');

req := CkHttpRequest_Create();
CkHttpRequest_AddParam(req,'grant_type','client_credentials');

CkHttpRequest_AddHeader(req,'x-merchant-id','7B3027');

CkHttpRequest_putHttpVerb(req,'POST');
CkHttpRequest_putContentType(req,'application/x-www-form-urlencoded');

resp := CkHttpResponse_Create();
success := CkHttp_HttpReq(http,'https://wwwcie.ups.com/security/v1/oauth/token',req,resp);
if (success = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

sbResponseBody := CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);

jResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,False);

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonObject__emit(jResp));

respStatusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkHttpResponse__header(resp));
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

// Save the OAuth2 access token for other examples to use.
CkJsonObject_WriteFile(jResp,'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"
// }

token_type := CkJsonObject__stringOf(jResp,'token_type');
issued_at := CkJsonObject__stringOf(jResp,'issued_at');
client_id := CkJsonObject__stringOf(jResp,'client_id');
access_token := CkJsonObject__stringOf(jResp,'access_token');
expires_in := CkJsonObject__stringOf(jResp,'expires_in');
status := CkJsonObject__stringOf(jResp,'status');

CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);

end;