Chilkat Online Tools

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

Back to Collection Items

integer li_rc
oleobject loo_Http
integer li_Success
oleobject loo_BdRequestBody
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
string ls_StartTimestamp
string ls_V_type
string ls_RoleDescription
string ls_ReferenceNumber
string ls_V_Type
string ls_ContextNameType
string ls_TradeName
string ls_Token
string ls_Timestamp
integer i
integer li_Count_i

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat_9_5_0.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_BdRequestBody = create oleobject
li_rc = loo_BdRequestBody.ConnectToNewObject("Chilkat_9_5_0.BinData")

li_Success = loo_BdRequestBody.LoadFile("file")
if li_Success <> 1 then
    Write-Debug "Failed to load file"
    destroy loo_Http
    destroy loo_BdRequestBody
    return
end if

loo_Http.SetRequestHeader("Accept","application/json")

loo_Resp = loo_Http.PBinaryBd("POST","https://ksef.mf.gov.pl/api/online/Session/InitToken",loo_BdRequestBody,"application/octet-stream",0,0)
if loo_Http.LastMethodSuccess = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_BdRequestBody
    return
end if

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

loo_Resp.GetBodySb(loo_SbResponseBody)

loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Resp.Header
    Write-Debug "Failed."
    destroy loo_Resp
    destroy loo_Http
    destroy loo_BdRequestBody
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

destroy loo_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

ls_ReferenceNumber = loo_JResp.StringOf("referenceNumber")
ls_V_Type = loo_JResp.StringOf("sessionToken.context.contextIdentifier.type")
ls_ContextNameType = loo_JResp.StringOf("sessionToken.context.contextName.type")
ls_TradeName = loo_JResp.StringOf("sessionToken.context.contextName.tradeName")
ls_Token = loo_JResp.StringOf("sessionToken.token")
ls_Timestamp = loo_JResp.StringOf("timestamp")
i = 0
li_Count_i = loo_JResp.SizeOfArray("sessionToken.context.credentialsRoleList")
do while i < li_Count_i
    loo_JResp.I = i
    ls_StartTimestamp = loo_JResp.StringOf("sessionToken.context.credentialsRoleList[i].startTimestamp")
    ls_V_type = loo_JResp.StringOf("sessionToken.context.credentialsRoleList[i].type")
    ls_RoleDescription = loo_JResp.StringOf("sessionToken.context.credentialsRoleList[i].roleDescription")
    i = i + 1
loop


destroy loo_Http
destroy loo_BdRequestBody
destroy loo_SbResponseBody
destroy loo_JResp

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