Chilkat Online Tools

C / ORACLE Hospitality OPERA Cloud REST API Workflows / 01. Get oAuth Token

Back to Collection Items

#include <C_CkHttp.h>
#include <C_CkHttpRequest.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkHttpRequest req;
    HCkJsonObject jsonParam1;
    HCkJsonObject jsonParam2;
    HCkHttpResponse resp;

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

    http = CkHttp_Create();

    CkHttp_putBasicAuth(http,TRUE);
    CkHttp_putLogin(http,"username");
    CkHttp_putPassword(http,"password");

    req = CkHttpRequest_Create();

    jsonParam1 = CkJsonObject_Create();
    CkHttpRequest_AddParam(req,"username",CkJsonObject_emit(jsonParam1));

    jsonParam2 = CkJsonObject_Create();
    CkHttpRequest_AddParam(req,"password",CkJsonObject_emit(jsonParam2));
    CkHttpRequest_AddParam(req,"grant_type","password");

    CkHttpRequest_AddHeader(req,"x-app-key","{{AppKey}}");

    resp = CkHttp_PostUrlEncoded(http,"https://domain.com/oauth/v1/tokens",req);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkHttpRequest_Dispose(req);
        CkJsonObject_Dispose(jsonParam1);
        CkJsonObject_Dispose(jsonParam2);
        return;
    }

    printf("%d\n",CkHttpResponse_getStatusCode(resp));
    printf("%s\n",CkHttpResponse_bodyStr(resp));
    CkHttpResponse_Dispose(resp);


    CkHttp_Dispose(http);
    CkHttpRequest_Dispose(req);
    CkJsonObject_Dispose(jsonParam1);
    CkJsonObject_Dispose(jsonParam2);

    }

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": "01. Get oAuth Token",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "var jsonData = JSON.parse(responseBody);\r",
          "postman.setEnvironmentVariable(\"Token\", jsonData.access_token);\r",
          "var jsonData = JSON.parse(responseBody);\r",
          "postman.setEnvironmentVariable(\"RefreshToken\", jsonData.refresh_token);"
        ],
        "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": [
  ]
}