Chilkat Online Tools

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

Back to Collection Items

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

Chilkat.Http http = new Chilkat.Http();
bool success;

http.BasicAuth = true;
http.Login = "username";
http.Password = "password";

Chilkat.HttpRequest req = new Chilkat.HttpRequest();

Chilkat.JsonObject jsonParam1 = new Chilkat.JsonObject();
req.AddParam("username",jsonParam1.Emit());

Chilkat.JsonObject jsonParam2 = new Chilkat.JsonObject();
req.AddParam("password",jsonParam2.Emit());
req.AddParam("grant_type","password");

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

Chilkat.HttpResponse resp = http.PostUrlEncoded("https://domain.com/oauth/v1/tokens",req);
if (http.LastMethodSuccess == false) {
    Debug.WriteLine(http.LastErrorText);
    return;
}

Debug.WriteLine(Convert.ToString(resp.StatusCode));
Debug.WriteLine(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": "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": [
  ]
}