Delphi DLL Fatturazione Elettronica Aruba IT: signin (Get an access_token)

Back to Index

Il metodo serve a richiedere un token di autenticazione. Tale token รจ necessario per invocare i metodi dei vari server della Fatturazione Elettronica (Resource Server) che sono protetti dal sistema.

Documentation: https://fatturazioneelettronica.aruba.it/apidoc/docs.html#_signin

CURL Command

curl -X POST  https://demoauth.fatturazioneelettronica.aruba.it/auth/signin \
-H 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8' \
-d 'grant_type=password' \
-d 'username=Utente' \
-d 'password=Password'

Delphi DLL Example

var
rest: HCkRest;
success: Boolean;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
strResponseBody: PWideChar;
respStatusCode: Integer;
jsonResponse: HCkJsonObject;
access_token: PWideChar;
token_type: PWideChar;
expires_in: Integer;
refresh_token: PWideChar;
client_id: PWideChar;
userName: PWideChar;
Issued: PWideChar;
Expires: PWideChar;

begin
rest := CkRest_Create();

//  URL: https://demoauth.fatturazioneelettronica.aruba.it/auth/signin
bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'demoauth.fatturazioneelettronica.aruba.it',port,bTls,bAutoReconnect);
if (success <> True) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(CkRest_getConnectFailReason(rest)));
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

CkRest_AddQueryParam(rest,'grant_type','password');
CkRest_AddQueryParam(rest,'username','Utente');
CkRest_AddQueryParam(rest,'password','Password');

strResponseBody := CkRest__fullRequestFormUrlEncoded(rest,'POST','/auth/signin');
if (CkRest_getLastMethodSuccess(rest) <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;
respStatusCode := CkRest_getResponseStatusCode(rest);
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkRest__responseHeader(rest));
    Memo1.Lines.Add('Response Body:');
    Memo1.Lines.Add(strResponseBody);
    Exit;
  end;

jsonResponse := CkJsonObject_Create();
CkJsonObject_Load(jsonResponse,strResponseBody);

//  See the Online Tool for Generating JSON Parse Code

access_token := CkJsonObject__stringOf(jsonResponse,'access_token');
token_type := CkJsonObject__stringOf(jsonResponse,'token_type');
expires_in := CkJsonObject_IntOf(jsonResponse,'expires_in');
refresh_token := CkJsonObject__stringOf(jsonResponse,'refresh_token');
client_id := CkJsonObject__stringOf(jsonResponse,'client_id');
userName := CkJsonObject__stringOf(jsonResponse,'userName');
Issued := CkJsonObject__stringOf(jsonResponse,'".issued"');
Expires := CkJsonObject__stringOf(jsonResponse,'".expires"');

CkRest_Dispose(rest);
CkJsonObject_Dispose(jsonResponse);

Sample JSON Response Body

{
  "access_token": "571a08cd6275e81e",
  "token_type": "bearer",
  "expires_in": 1800,
  "refresh_token": "262ac1f88ad1e9e5",
  "client_id": "123456",
  "userName": "Utente",
  ".issued": "Mon, 21 Jan 2019 16:36:17 GMT",
  ".expires": "Mon, 21 Jan 2019 17:06:17 GMT"
}