Back to Collection Items
var
http: TChilkatHttp;
success: Integer;
req: TChilkatHttpRequest;
jsonParam1: TChilkatJsonObject;
jsonParam2: TChilkatJsonObject;
resp: IChilkatHttpResponse;
begin
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := TChilkatHttp.Create(Self);
http.BasicAuth := 1;
http.Login := 'username';
http.Password := 'password';
req := TChilkatHttpRequest.Create(Self);
jsonParam1 := TChilkatJsonObject.Create(Self);
req.AddParam('username',jsonParam1.Emit());
jsonParam2 := TChilkatJsonObject.Create(Self);
req.AddParam('password',jsonParam2.Emit());
req.AddParam('grant_type','password');
req.AddHeader('x-app-key','{{AppKey}}');
resp := http.PostUrlEncoded('https://domain.com/oauth/v1/tokens',req.ControlInterface);
if (http.LastMethodSuccess = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
Exit;
end;
Memo1.Lines.Add(IntToStr(resp.StatusCode));
Memo1.Lines.Add(resp.BodyStr);
Curl Command
curl -X POST
-u 'username:password'
-H "Content-Type: application/x-www-form-urlencoded"
-H "x-app-key: {{AppKey}}"
--data-urlencode 'username={{Username}}'
--data-urlencode 'password={{Password}}'
--data-urlencode 'grant_type=password'
https://domain.com/oauth/v1/tokens
Postman Collection Item JSON
{
"name": "Get OAuth Token //initialiize currentdate",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var jsonData = JSON.parse(responseBody);\r",
"postman.setEnvironmentVariable(\"Token\", jsonData.access_token);\r",
"//Set currentdate and currentdateplus1 variables\r",
"var moment = require('moment');\r",
"pm.environment.set('currentdate', moment().format((\"YYYY-MM-DD\")))\r",
"pm.environment.set('currentdateplus1', moment().add(1, 'days').format((\"YYYY-MM-DD\")))"
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "basic",
"basic": [
{
"key": "password",
"value": "{{CLIENT_SECRET}}",
"type": "string"
},
{
"key": "username",
"value": "{{CLIENT_ID}}",
"type": "string"
},
{
"key": "saveHelperData",
"value": true,
"type": "boolean"
},
{
"key": "showPassword",
"value": false,
"type": "boolean"
}
]
},
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/x-www-form-urlencoded"
},
{
"key": "x-app-key",
"type": "text",
"value": "{{AppKey}}"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "username",
"value": "{{Username}}",
"type": "text"
},
{
"key": "password",
"value": "{{Password}}",
"type": "text"
},
{
"key": "grant_type",
"value": "password",
"type": "text"
}
]
},
"url": {
"raw": "{{HostName}}/oauth/v1/tokens",
"host": [
"{{HostName}}"
],
"path": [
"oauth",
"v1",
"tokens"
]
}
},
"response": [
]
}