nekbmm wrote: ↑Sun Mar 21, 2021 7:36 pm
How can I access these data?
Oh, I was expecting a different response (that's how it is, if I don't know Serbian

)
The AccesToken is included in the "payload" key. First, the value of the "payload" key should be read, we should receive another Hash, and from this Hash we read the "accessToken" key.
Check out this code snippet:
Code: Select all
//login
cApiFunction := "/api/login"
//prepare json
hAtributi := { => }
hAtributi ["login"] := "foo"
hAtributi ["password"] := "bar"
strfile (hb_jsonEncode( hAtributi , .F. ), 'login_json.txt')
cResp := SendRestApi( cUrl + cApiFunction, hb_jsonEncode( hAtributi , .F. ), oRestApi )
IF cResp = "!ERROR!"
MsgStop (cResp)
oRestApi := Nil
Quit
ENDIF
IF oRestApi:Status <> 200
MsgStop ( "HTTP status: " + hb_NToS(oRestApi:status) + " " +oRestApi:statusText + CRLF + CRLF + "Response: " + cResp )
ENDIF
hResp := hb_jsonDecode( cResp )
hStatus := hb_HGetDef( hResp , "status", { => } )
msginfo ("Message :" + hb_HGetDef( hStatus , "message", "" ) + CRLF + "Code :" + hb_ValToStr ( hb_HGetDef( hStatus , "code", "" ) ) )
hPayload := hb_HGetDef( hResp , "payload", { => } )
cCreationTime := hb_HGetDef( hPayload , "creationTime", "" )
cAccessToken := hb_HGetDef( hPayload , "accessToken", "" )
cRefreshToken := hb_HGetDef( hPayload , "refreshToken", "" )
msginfo ( "creationTime: " + cCreationTime + CRLF + ;
"accessToken: " + cAccessToken + CRLF + ;
"refreshToken: " + cRefreshToken )
IF Empty ( cAccessToken )
MsgStop ("No accessToken, no authorization !!!")
ENDIF
I am not sure about the authorization yet, according to the description, the accessToken should be sent in the header "Authorization" as
Bearer <accessToken>, while in Swagger it is sent without the word "
Bearer". If the authorization does not work, try changing to:
Code: Select all
oRestApi:setRequestHeader ("Authorization", cAuth)