var
tokenUser: string;
Response, ChallengeID, TimeStamp, TokenSesji, ReferenceNumber, xmlBody: string;
JsonValue: TJSONValue;
StatusCode:integer;
function myStrToUnixDateTime(sData: string): string;
var Data: TDateTime;
ms : int64;
r,m,d,g,n,s,z: integer;
begin
Result := '';
try
r := StrToInt(Copy(sData, 1,4));
m := StrToInt(Copy(sData, 6,2));
d := StrToInt(Copy(sData, 9,2));
g := StrToInt(Copy(sData, 12,2));
n := StrToInt(Copy(sData, 15,2));
s := StrToInt(Copy(sData, 18,2));
z := StrToInt(Copy(sData, 21,3));
Data := EncodeDateTime(r,m,d,g,n,s,z);
ms := Round((Data - UnixDateDelta) * SecsPerDay*1000);
Result := IntToStr(ms);
except
end;
end;
procedure TForm1.AuthorisationChallenge;
begin
StatusCode := 0;
RRequest.Accept := 'application/json';
RRequest.Resource := challengeResource;
RRequest.body.Add(format(challengeJSON, [NIPFirmy]), trestcontenttype.ctAPPLICATION_JSON);
RRequest.Method := rmpost;
RRequest.Execute;
StatusCode := RResponse.StatusCode;
Memo1.Lines.Clear;
Memo1.Lines.Add(RResponse.Content);
if StatusCode = 201 then
begin
JSONValue := TJSONObject.ParseJSONValue(RResponse.Content);
JSONValue.TryGetValue('challenge', ChallengeID);
JSONValue.TryGetValue('timestamp', TimeStamp);
end;
end;
procedure TForm1.InitToken;
begin
StatusCode := 0;
Memo1.Lines.Clear;
Memo1.Text := tokenUser+'|'+myStrToUnixDateTime(timestamp);
Memo1.Lines.SaveToFile('tekst_in.txt');
DeleteFile('base64_out.txt');
winexec('encryptBase64.bat',sw_Normal);//szyfruję za pomocą OpenSSL.exe
if chTylkoToken.Checked then Exit;
sleep(1000);
if FileExists('base64_out.txt') then begin
Memo1.Lines.Clear;
Memo1.Lines.LoadFromFile('base64_out.txt');
tokenSesji := Memo1.Text;
tokenSesji := StringReplace(tokenSesji, #13,'',[rfReplaceAll]);
tokenSesji := StringReplace(tokenSesji, #10,'',[rfReplaceAll]);
xmlBody := format(initTokenXML_1, [ChallengeID, NIPFirmy, tokenSesji]);
Memo1.Lines.Clear;
Memo1.Lines.Text := xmlBody;
Memo1.Lines.SaveToFile('initToken.xml');
if chTylkoXML.Checked then exit;
sleep(1000);
RRequest.Accept := 'application/json';
RRequest.AcceptCharset := 'utf-8';
RRequest.Body.ClearBody;
RRequest.AddBody(xmlBody, trestcontenttype.ctAPPLICATION_OCTET_STREAM);
RRequest.Resource := 'online/Session/InitToken';
RRequest.Method := rmPost;
RRequest.Execute;
StatusCode := RResponse.StatusCode;
Memo1.Lines.Clear;
Memo1.Lines.Add(RResponse.Content);
if StatusCode = 201 then
begin
JSONValue := TJSONObject.ParseJSONValue(RResponse.Content);
JSONValue.TryGetValue('sessionToken.token', tokenSesji);
JSONValue.TryGetValue('referenceNumber', referenceNumber);
end;
end;
end;
RRequest, RResponse i RClient położyłem na formularzu i powiązałem , nie zmieniałem żadnych ustawień oprócz wpisania w BaseUrl: https://ksef-test.mf.gov.pl/api.
- Wywołuję
AuthorisationChallenge
i potem InitToken
i zwraca błąd niezgodności z xsd
- Wywołuję
AuthorisationChallenge
i potemInitToken
i przerywam: tworzę initToken.xml wklejam do Postman, wykonuję i zwraca StatusCode 201
Próbowałem z różnymi ustawieniami string, ansistring, AnsiToUtf8, ale nic to nie daje.
Przy innym typie niż ctAPPLICATION_OCTET_STREAM np. ctAPPLICATION_XML zwraca błąd złej zawartości.
Może coś we właściwościach RRequest, RResponse, RClient trzeba ustawić?