Chilkat Online Tools

PureBasic / KSeF / Inicjalizacja sesji, wskazanie kontekstu, uwierzytelnienie i autoryzacja

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkBinData.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

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

    success = CkBinData::ckLoadFile(bdRequestBody,"file")
    If success <> 1
        Debug "Failed to load file"
        CkHttp::ckDispose(http)
        CkBinData::ckDispose(bdRequestBody)
        ProcedureReturn
    EndIf

    CkHttp::ckSetRequestHeader(http,"Accept","application/json")

    resp.i = CkHttp::ckPBinaryBd(http,"POST","https://ksef.mf.gov.pl/api/online/Session/InitToken",bdRequestBody,"application/octet-stream",0,0)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkBinData::ckDispose(bdRequestBody)
        ProcedureReturn
    EndIf

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

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)

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

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttpResponse::ckDispose(resp)

        CkHttp::ckDispose(http)
        CkBinData::ckDispose(bdRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    CkHttpResponse::ckDispose(resp)

    ; Sample JSON response:
    ; (Sample code for parsing the JSON response is shown below)

    ; {
    ;   "referenceNumber": "28201005-FY-586FD2AD02-40A998FD15-B2",
    ;   "sessionToken": {
    ;     "context": {
    ;       "contextIdentifier": {
    ;         "type": "<string>"
    ;       },
    ;       "credentialsRoleList": [
    ;         {
    ;           "startTimestamp": "<dateTime>",
    ;           "type": "<string>",
    ;           "roleDescription": "<string>",
    ;           "roleType": {}
    ;         }
    ;       ],
    ;       "contextName": {
    ;         "type": "<string>",
    ;         "tradeName": "<string>"
    ;       }
    ;     },
    ;     "token": "<string>"
    ;   },
    ;   "timestamp": "<dateTime>"
    ; }

    ; Sample code for parsing the JSON response...
    ; Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    startTimestamp.s
    v_type.s
    roleDescription.s

    referenceNumber.s = CkJsonObject::ckStringOf(jResp,"referenceNumber")
    v_Type.s = CkJsonObject::ckStringOf(jResp,"sessionToken.context.contextIdentifier.type")
    ContextNameType.s = CkJsonObject::ckStringOf(jResp,"sessionToken.context.contextName.type")
    TradeName.s = CkJsonObject::ckStringOf(jResp,"sessionToken.context.contextName.tradeName")
    Token.s = CkJsonObject::ckStringOf(jResp,"sessionToken.token")
    timestamp.s = CkJsonObject::ckStringOf(jResp,"timestamp")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"sessionToken.context.credentialsRoleList")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        startTimestamp = CkJsonObject::ckStringOf(jResp,"sessionToken.context.credentialsRoleList[i].startTimestamp")
        v_type = CkJsonObject::ckStringOf(jResp,"sessionToken.context.credentialsRoleList[i].type")
        roleDescription = CkJsonObject::ckStringOf(jResp,"sessionToken.context.credentialsRoleList[i].roleDescription")
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkBinData::ckDispose(bdRequestBody)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	-H "Content-Type: application/octet-stream"
	-H "Accept: application/json"
	--data-binary '@file'
https://ksef.mf.gov.pl/api/online/Session/InitToken

Postman Collection Item JSON

{
  "name": "Inicjalizacja sesji, wskazanie kontekstu, uwierzytelnienie i autoryzacja",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/octet-stream"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "file",
      "file": {}
    },
    "url": {
      "raw": "{{baseUrl}}/online/Session/InitToken",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "online",
        "Session",
        "InitToken"
      ]
    },
    "description": "Inicjalizacja sesji interaktywnej. Zaszyfrowany kluczem publicznym KSeF dokument http://ksef.mf.gov.pl/schema/gtw/svc/online/auth/request/2021/10/01/0001/InitSessionTokenRequest"
  },
  "response": [
    {
      "name": "Inicjalizacja sesji interaktywnej poprawna",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/octet-stream"
          },
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "file",
          "file": {}
        },
        "url": {
          "raw": "{{baseUrl}}/online/Session/InitToken",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "online",
            "Session",
            "InitToken"
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"referenceNumber\": \"28201005-FY-586FD2AD02-40A998FD15-B2\",\n  \"sessionToken\": {\n    \"context\": {\n      \"contextIdentifier\": {\n        \"type\": \"<string>\"\n      },\n      \"credentialsRoleList\": [\n        {\n          \"startTimestamp\": \"<dateTime>\",\n          \"type\": \"<string>\",\n          \"roleDescription\": \"<string>\",\n          \"roleType\": {}\n        }\n      ],\n      \"contextName\": {\n        \"type\": \"<string>\",\n        \"tradeName\": \"<string>\"\n      }\n    },\n    \"token\": \"<string>\"\n  },\n  \"timestamp\": \"<dateTime>\"\n}"
    },
    {
      "name": "Nieprawidłowe wywołanie",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/octet-stream"
          },
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "file",
          "file": {}
        },
        "url": {
          "raw": "{{baseUrl}}/online/Session/InitToken",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "online",
            "Session",
            "InitToken"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"exception\": {\n    \"exceptionDetailList\": [\n      {\n        \"exceptionCode\": \"<integer>\",\n        \"exceptionDescription\": \"<string>\"\n      }\n    ],\n    \"serviceCode\": \"<string>\",\n    \"serviceCtx\": \"<string>\",\n    \"serviceName\": \"<string>\",\n    \"timestamp\": \"<dateTime>\",\n    \"referenceNumber\": \"20560508-TX-BC1107E227-31DE9417C0-CE\"\n  }\n}"
    }
  ]
}