Chilkat Online Tools

PureBasic / Egyptian eInvoicing SDK R1.5 / 1. Authenticate POS

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

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

    CkHttpRequest::ckAddParam(req,"grant_type","client_credentials")

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

    CkHttpRequest::ckAddParam(req,"client_id",CkJsonObject::ckEmit(jsonParam2))

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

    CkHttpRequest::ckAddParam(req,"client_secret",CkJsonObject::ckEmit(jsonParam3))

    CkHttpRequest::ckAddHeader(req,"presharedkey","C68FEC13-FE34-4839-89F9-ADB0D678CFE0")
    CkHttpRequest::ckAddHeader(req,"posmodelframework","1")
    CkHttpRequest::ckAddHeader(req,"posserial","Test_SerialNo1")
    CkHttpRequest::ckAddHeader(req,"pososversion","IOS")

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

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



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


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	-H "posserial: Test_SerialNo1"
	-H "pososversion: IOS"
	-H "posmodelframework: 1"
	-H "presharedkey: C68FEC13-FE34-4839-89F9-ADB0D678CFE0"
	--data-urlencode 'grant_type=client_credentials'
	--data-urlencode 'client_id={{clientId}}'
	--data-urlencode 'client_secret={{clientSecret}}'
https://domain.com/connect/token

Postman Collection Item JSON

{
  "name": "1. Authenticate POS",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "var json = pm.response.json()\r",
          "var accesstoken=json.access_token\r",
          "pm.environment.set(\"generatedAccessToken\",accesstoken);\r",
          "\r",
          "pm.test(\"Status code is 200\", function () {\r",
          "    pm.response.to.have.status(200);\r",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "posserial",
        "value": "Test_SerialNo1",
        "type": "text"
      },
      {
        "key": "pososversion",
        "value": "IOS",
        "type": "text"
      },
      {
        "key": "posmodelframework",
        "value": "1",
        "type": "text",
        "disabled": true
      },
      {
        "key": "presharedkey",
        "value": "C68FEC13-FE34-4839-89F9-ADB0D678CFE0",
        "type": "text"
      }
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "grant_type",
          "value": "client_credentials",
          "type": "text"
        },
        {
          "key": "client_id",
          "value": "{{clientId}}",
          "type": "text"
        },
        {
          "key": "client_secret",
          "value": "{{clientSecret}}",
          "type": "text"
        }
      ]
    },
    "url": {
      "raw": "{{idSrvBaseUrl}}/connect/token",
      "host": [
        "{{idSrvBaseUrl}}"
      ],
      "path": [
        "connect",
        "token"
      ]
    }
  },
  "response": [
  ]
}