Chilkat Online Tools

SQL Server / KSeF / Inicjalizacja sesji, wskazanie kontekstu, uwierzytelnienie i autoryzacja

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
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    DECLARE @bdRequestBody int
    -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdRequestBody OUT

    EXEC sp_OAMethod @bdRequestBody, 'LoadFile', @success OUT, 'file'
    IF @success <> 1
      BEGIN

        PRINT 'Failed to load file'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bdRequestBody
        RETURN
      END

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PBinaryBd', @resp OUT, 'POST', 'https://ksef.mf.gov.pl/api/online/Session/InitToken', @bdRequestBody, 'application/octet-stream', 0, 0
    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 @bdRequestBody
        RETURN
      END

    DECLARE @sbResponseBody int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bdRequestBody
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @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

    DECLARE @startTimestamp nvarchar(4000)

    DECLARE @v_type nvarchar(4000)

    DECLARE @roleDescription nvarchar(4000)

    DECLARE @referenceNumber nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @referenceNumber OUT, 'referenceNumber'
    DECLARE @v_Type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'sessionToken.context.contextIdentifier.type'
    DECLARE @ContextNameType nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ContextNameType OUT, 'sessionToken.context.contextName.type'
    DECLARE @TradeName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @TradeName OUT, 'sessionToken.context.contextName.tradeName'
    DECLARE @Token nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Token OUT, 'sessionToken.token'
    DECLARE @timestamp nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @timestamp OUT, 'timestamp'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'sessionToken.context.credentialsRoleList'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @startTimestamp OUT, 'sessionToken.context.credentialsRoleList[i].startTimestamp'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'sessionToken.context.credentialsRoleList[i].type'
        EXEC sp_OAMethod @jResp, 'StringOf', @roleDescription OUT, 'sessionToken.context.credentialsRoleList[i].roleDescription'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @bdRequestBody
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

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