Unicode C++ 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'

Unicode C++ Example

#include <CkRestW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    CkRestW rest;
    bool success;

    //  URL: https://demoauth.fatturazioneelettronica.aruba.it/auth/signin
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    success = rest.Connect(L"demoauth.fatturazioneelettronica.aruba.it",port,bTls,bAutoReconnect);
    if (success != true) {
        wprintf(L"ConnectFailReason: %d\n",rest.get_ConnectFailReason());
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    rest.AddQueryParam(L"grant_type",L"password");
    rest.AddQueryParam(L"username",L"Utente");
    rest.AddQueryParam(L"password",L"Password");

    const wchar_t *strResponseBody = rest.fullRequestFormUrlEncoded(L"POST",L"/auth/signin");
    if (rest.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    int respStatusCode = rest.get_ResponseStatusCode();
    if (respStatusCode >= 400) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",rest.responseHeader());
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",strResponseBody);
        return;
    }

    CkJsonObjectW jsonResponse;
    jsonResponse.Load(strResponseBody);

    //  See the Online Tool for Generating JSON Parse Code
    const wchar_t *access_token = 0;
    const wchar_t *token_type = 0;
    int expires_in;
    const wchar_t *refresh_token = 0;
    const wchar_t *client_id = 0;
    const wchar_t *userName = 0;
    const wchar_t *Issued = 0;
    const wchar_t *Expires = 0;

    access_token = jsonResponse.stringOf(L"access_token");
    token_type = jsonResponse.stringOf(L"token_type");
    expires_in = jsonResponse.IntOf(L"expires_in");
    refresh_token = jsonResponse.stringOf(L"refresh_token");
    client_id = jsonResponse.stringOf(L"client_id");
    userName = jsonResponse.stringOf(L"userName");
    Issued = jsonResponse.stringOf(L"\".issued\"");
    Expires = jsonResponse.stringOf(L"\".expires\"");
    }

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