Chilkat Online Tools

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

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttpRequest.pb"

Procedure ChilkatExample()

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    CkHttp::setCkBasicAuth(http, 1)
    CkHttp::setCkLogin(http, "username")
    CkHttp::setCkPassword(http, "password")

    req.i = CkHttpRequest::ckCreate()
    If req.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonParam1.i = CkJsonObject::ckCreate()
    If jsonParam1.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpRequest::ckAddParam(req,"username",CkJsonObject::ckEmit(jsonParam1))

    jsonParam2.i = CkJsonObject::ckCreate()
    If jsonParam2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpRequest::ckAddParam(req,"password",CkJsonObject::ckEmit(jsonParam2))
    CkHttpRequest::ckAddParam(req,"grant_type","password")

    CkHttpRequest::ckAddHeader(req,"x-app-key","{{AppKey}}")

    resp.i = CkHttp::ckPostUrlEncoded(http,"https://domain.com/oauth/v1/tokens",req)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        CkJsonObject::ckDispose(jsonParam1)
        CkJsonObject::ckDispose(jsonParam2)
        ProcedureReturn
    EndIf

    Debug Str(CkHttpResponse::ckStatusCode(resp))
    Debug CkHttpResponse::ckBodyStr(resp)
    CkHttpResponse::ckDispose(resp)



    CkHttp::ckDispose(http)
    CkHttpRequest::ckDispose(req)
    CkJsonObject::ckDispose(jsonParam1)
    CkJsonObject::ckDispose(jsonParam2)


    ProcedureReturn
EndProcedure

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": [
  ]
}