Chilkat Online Tools

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

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    EXEC sp_OASetProperty @http, 'BasicAuth', 1
    EXEC sp_OASetProperty @http, 'Login', 'username'
    EXEC sp_OASetProperty @http, 'Password', 'password'

    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.HttpRequest', @req OUT

    DECLARE @jsonParam1 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonParam1 OUT

    EXEC sp_OAMethod @jsonParam1, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'username', @sTmp0

    DECLARE @jsonParam2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonParam2 OUT

    EXEC sp_OAMethod @jsonParam2, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'password', @sTmp0
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'grant_type', 'password'

    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'x-app-key', '{{AppKey}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostUrlEncoded', @resp OUT, 'https://domain.com/oauth/v1/tokens', @req
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @jsonParam1
        EXEC @hr = sp_OADestroy @jsonParam2
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @jsonParam1
    EXEC @hr = sp_OADestroy @jsonParam2


END
GO

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