PureBasic 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'

PureBasic Example

IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ;  URL: https://demoauth.fatturazioneelettronica.aruba.it/auth/signin
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"demoauth.fatturazioneelettronica.aruba.it",port,bTls,bAutoReconnect)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    CkRest::ckAddQueryParam(rest,"grant_type","password")
    CkRest::ckAddQueryParam(rest,"username","Utente")
    CkRest::ckAddQueryParam(rest,"password","Password")

    strResponseBody.s = CkRest::ckFullRequestFormUrlEncoded(rest,"POST","/auth/signin")
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkRest::ckResponseStatusCode(rest)
    If respStatusCode >= 400
        Debug "Response Status Code = " + Str(respStatusCode)
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug strResponseBody
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    jsonResponse.i = CkJsonObject::ckCreate()
    If jsonResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(jsonResponse,strResponseBody)

    ;  See the Online Tool for Generating JSON Parse Code
    access_token.s
    token_type.s
    expires_in.i
    refresh_token.s
    client_id.s
    userName.s
    Issued.s
    Expires.s

    access_token = CkJsonObject::ckStringOf(jsonResponse,"access_token")
    token_type = CkJsonObject::ckStringOf(jsonResponse,"token_type")
    expires_in = CkJsonObject::ckIntOf(jsonResponse,"expires_in")
    refresh_token = CkJsonObject::ckStringOf(jsonResponse,"refresh_token")
    client_id = CkJsonObject::ckStringOf(jsonResponse,"client_id")
    userName = CkJsonObject::ckStringOf(jsonResponse,"userName")
    Issued = CkJsonObject::ckStringOf(jsonResponse,Chr(34) + ".issued" + Chr(34))
    Expires = CkJsonObject::ckStringOf(jsonResponse,Chr(34) + ".expires" + Chr(34))


    CkRest::ckDispose(rest)
    CkJsonObject::ckDispose(jsonResponse)


    ProcedureReturn
EndProcedure

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