B4J Question Send HTTP POST request with multiple answers

CR95

Active Member
Licensed User
Hello,
I would like to connect my B4J Client program to a box on the same LAN (this client is running today on Windows and later will be on a Raspberry).
Specifications of dialog with the box are clear, but one of them is an issue as there are several answers (depending on the user who has to click on a button on the box to authorize the request)
Track authorization progress
Once the authorization request has been made, the app should monitor the token status by using the following API and using the track_id returned by the previous call.
The status can have one of the following values:
StatusDescription
unknownthe app_token is invalid or has been revoked
pendingthe user has not confirmed the authorization request yet
timeoutthe user did not confirmed the authorization within the given time
grantedthe app_token is valid and can be used to open a session
deniedthe user denied the authorization request
The app should monitor the status until it is different from pending.
GET /api/v4/login/authorize/{track_id}
Example request:
GET /api/v4/login/authorize/42 HTTP/1.1
Host: mafreebox.freebox.fr

Example response:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"success": true,
"result": {
"status": "pending",
"challenge": "Bj6xMqoe+DCHD44KqBljJ579seOXNWr2"
}
}
Example response once the user has validated the request:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
"success": true,
"result": {
"status": "granted",
"challenge": "Bj6xMqoe+DCHD44KqBljJ579seOXNWr2"
}
}
Because (I presume) there will be multiple sequential responses, I do not use the "Wait For (SendHTTPPost(url, data)) complete (Response As String)" as in the previous exchanges.
My understanding is that only works for one (immediate) unique response.
That is why I use the following code :
Dim MyJob As HttpJob
MyJob.Initialize("",Me)
Dim URL As String = urlAuthorize & track_id
MyJob.Download(URL)
MyJob.GetRequest.SetHeader("VersionList","HTTP/1.1")
.....
Sub JobDone(job As HttpJob)
Log("Entry dans JobDone= " & job.Response)
If job.Success = False Then
Log("Erreur get_authorization_status= " & job.Response)
End If
' The result is a json string. We parse it and log the fields.
Dim Parser As JSONParser
Parser.Initialize(job.Response)
Dim root As Map
root = Parser.NextObject
Dim level2 As Map
level2 = root.Get("result")
authorization_progress = level2.Get("status")
Log("authorization_progress= " & authorization_progress)
Dim ErrMessage As String
Select authorization_progress
Case "granted"
job.Release
Case "denied"
ErrMessage = "denied - authorization failed"
Log(ErrMessage)
Return
Case "pending"
ErrMessage = "pending - user must accept the app request on the freebox"
Log(ErrMessage)
Return
Case "timeout"
ErrMessage = "timeout - Please confirm the authentification on the freebox"
Log(ErrMessage)
Return
Case Else
ErrMessage = "Retour inattendu après demande d'autorisation"
Log(ErrMessage)
Return
End Select
The unique response that is received is following error
6) Get request for track. Adresse= https://oq9ctldw.fbxos.fr:29708/api/v8/login/authorize/11
Entry dans JobDone= anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpResponse@186f038d
main._jobdone (java line: 541)
java.lang.RuntimeException: JSON Object expected.
at anywheresoftware.b4j.objects.collections.JSONParser.NextObject(JSONParser.java:50)
at b4j.example.main._jobdone(main.java:541)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:487)
at anywheresoftware.b4a.keywords.Common.access$0(Common.java:467)
at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:541)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Yes. My program is waiting for a JSON string !
Please could you tell me if my process is the good one.
If yes, what should I do to get the good answer ?
Thanks for your help
 

rosippc64a

Active Member
Licensed User
Longtime User
try instead of
B4X:
Dim Parser As JSONParser
Parser.Initialize(job.Response)
this:
B4X:
Dim Parser As JSONParser
Parser.Initialize(job.Getstring)
 
Upvote 0

CR95

Active Member
Licensed User
Thanks for your answer.
Yes, by applying your suggestion, I get now a "pending" response from the server.
Then I run to my box and I click on the button to finish the authorization process.
But nothing happens. It seems that my program does not receive the following response (which should be "granted").
My understanding was that a new event will fire the sub "jobdone" without additional code. It seems that I am wrong
Do I have to resend a complete request (httpjob initialize/ download / jobdone....) in order to get the second response ?
 
Upvote 0
Top