Android / KSeF / Inicjalizacja sesji, wskazanie kontekstu, uwierzytelnienie i autoryzacja
Back to Collection Items
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttp http = new CkHttp();
boolean success;
CkBinData bdRequestBody = new CkBinData();
success = bdRequestBody.LoadFile("file");
if (success != true) {
Log.i(TAG, "Failed to load file");
return;
}
http.SetRequestHeader("Accept","application/json");
CkHttpResponse resp = http.PBinaryBd("POST","https://ksef.mf.gov.pl/api/online/Session/InitToken",bdRequestBody,"application/octet-stream",false,false);
if (http.get_LastMethodSuccess() == false) {
Log.i(TAG, http.lastErrorText());
return;
}
CkStringBuilder sbResponseBody = new CkStringBuilder();
resp.GetBodySb(sbResponseBody);
CkJsonObject jResp = new CkJsonObject();
jResp.LoadSb(sbResponseBody);
jResp.put_EmitCompact(false);
Log.i(TAG, "Response Body:");
Log.i(TAG, jResp.emit());
int respStatusCode = resp.get_StatusCode();
Log.i(TAG, "Response Status Code = " + String.valueOf(respStatusCode));
if (respStatusCode >= 400) {
Log.i(TAG, "Response Header:");
Log.i(TAG, resp.header());
Log.i(TAG, "Failed.");
return;
}
// 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
String startTimestamp;
String v_type;
String roleDescription;
String referenceNumber = jResp.stringOf("referenceNumber");
String v_Type = jResp.stringOf("sessionToken.context.contextIdentifier.type");
String ContextNameType = jResp.stringOf("sessionToken.context.contextName.type");
String TradeName = jResp.stringOf("sessionToken.context.contextName.tradeName");
String Token = jResp.stringOf("sessionToken.token");
String timestamp = jResp.stringOf("timestamp");
int i = 0;
int count_i = jResp.SizeOfArray("sessionToken.context.credentialsRoleList");
while (i < count_i) {
jResp.put_I(i);
startTimestamp = jResp.stringOf("sessionToken.context.credentialsRoleList[i].startTimestamp");
v_type = jResp.stringOf("sessionToken.context.credentialsRoleList[i].type");
roleDescription = jResp.stringOf("sessionToken.context.credentialsRoleList[i].roleDescription");
i = i + 1;
}
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}
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}"
}
]
}