B4J Question Help with Parse

derez

Expert
Licensed User
Longtime User
I made a Family Tree app which updates the DB at several users using PARSE.
It works nice and I made an equivalent app with B4j but the PARSE lib is not compatible.
Agraham showed me their REST API https://parse.com/docs/rest#users-login
It looks like something that can be done using HTTP and HTTPUTILS but I have no experience. The examples are in PYTHON or CURL.
For example : login process
B4X:
import json,httplib,urllib
connection = httplib.HTTPSConnection('api.parse.com', 443)
params = urllib.urlencode({"username":"cooldude6","password":"p_n7!-e8"})
connection.connect()
connection.request('GET', '/1/login?%s' % params, '', {
"X-Parse-Application-Id": "${APPLICATION_ID}",
"X-Parse-REST-API-Key": "${REST_API_KEY}"
})
result = json.loads(connection.getresponse().read())
print result

I made a successful connection :
B4X:
skt.Initialize("skt")
skt.Connect("api.parse.com", 443,0)

But got lost afterwards. I guess it should be job1.download2() with the username & password (which I have).
After login I need to get objects and send objects.
If someone could help – I’ll appreciate.
 

thedesolatesoul

Expert
Licensed User
Longtime User
I will venture a guess, but here is what I think you need.
Use the HttpClient (atleast initially)
You need to set the headers:
B4X:
HttpC.SetHeader("X-Parse-Application-Id", you app id)
HttpC.SetHeader("X-Parse-REST-API-Key", you reset api key from parse)
HttpC.InitializeGet ("http://api.parse.com/1/login?username=" & UrlEncode(username) & "&password=" & UrlEncode(password))
 
Upvote 0

derez

Expert
Licensed User
Longtime User
I wrote this:
B4X:
If Successful Then
    Log("connected")
    HR.InitializeHead("api.parse.com/1/users")
    HR.SetHeader("X-Parse-Application-Id", "xxxxxxx")
    HR.SetHeader("X-Parse-REST-API-Key","xxxxx")
  
    Dim username As String = su.EncodeUrl("xxxxxxx","UTF8")
    Dim pass As String = su.EncodeUrl("xxxxx","UTF8")
    HR.InitializeGet("http://api.parse.com/1/login?username=" & username & "&password=" & pass)
    hc.Execute(HR,1)
End If
end sub

Sub hc_ResponseError (Reason As String, StatusCode As Int, TaskId As Int)
    Log(Reason &  "  =error")
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Log(Response.GetString("UTF8") &  "   success")
End Sub

I get this:
Program started.
connected
An error occurred:
(Line: 40) End Sub
java.lang.Exception: Sub hc_responseerror signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject b4j.example.main_subs_0._hc_responseerror(anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception
 
Upvote 0

derez

Expert
Licensed User
Longtime User
This sub works and I get an error response: "unauthorized". The same with Https.
I'll have to check the data I submit.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
May be, I added hc.Execute(HR,0) after setting the headers, now I get for this execution:
java.lang.IllegalStateException: Target host must not be null, or set in parameters.
This appears as red lines from the compiler and also as a response from the site.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Implementation of the signup API: https://parse.com/docs/rest#users-signup

B4X:
Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   'MainForm.RootPane.LoadLayout("1") 'Load the layout file.
   MainForm.Show
   Dim j As HttpJob
   j.Initialize("log-in", Me)
   Dim user As Map
   user.Initialize
   user.Put("username", "user1")
   user.Put("password", "123456")
   Dim gen As JSONGenerator
   gen.Initialize(user)   
   j.PostString("https://api.parse.com/1/users", gen.ToString)
   j.GetRequest.SetHeader("X-Parse-REST-API-Key", "xxx")
   j.GetRequest.SetHeader("X-Parse-Application-Id", "yyy")
   j.GetRequest.SetContentType("application/json")
End Sub

Sub JobDone(job As HttpJob)
   Log(job)
   If job.Success Then
     Log(job.GetString)
   End If
   job.Release
End Sub
Depends on: HttpUtils2, Http and JSON libraries.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Wow that works - I got connected and logged in.
Please show me how to send and recieve objects (lines of the DB in parse), like this in B4A:
B4X:
....Dim CheckOne As ParseQuery
CheckOne.Initialize("Family")
Dim k As Int
k = strg(0)
CheckOne.WhereEqualTo("No",k)
CheckOne.Find("CheckOne", k) ....

Sub CheckOne_DoneFind (Success As Boolean, ListOfPO As List, TaskId As Int)
'Log("CheckOne_DoneFind ")
If Success = False Then
    Log("Error: " & LastException.Message)
Else
    Dim po As ParseObject
    If ListOfPO.Size > 0 Then
        po = ListOfPO.Get(0)
   Else
        parse_add_item(strg(0))
    End If
End If
End Sub
 
Upvote 0

derez

Expert
Licensed User
Longtime User
The second time and on that I run the program I get:
Program started.
{"code":202,"error":"username [email protected] already taken"}
[jobname=log-in, success=false, username=
, password=, errormessage=Bad Request, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper@9438dd, tag=java.lang.Object@1216ee9
, fx=anywheresoftware.b4j.objects.JFX@1f88cce, httputils2service=null]

But a new user line was written in parse and maybe there is no need to do it again afterwards, just continue.
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
Erel, I see what happened. you translated the sign-in process, once it was done -no need to do it again.
now I need the login process which is written in the first post.
I tried by changing to https://api.parse.com/1/login but get a 404 answer (when I go to this address in browser I get a form for input my username and password).
 

Attachments

  • parse.zip
    940 bytes · Views: 324
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
with download2, array contains the username and password, I get response of "unauthorized".
This is probably because the request does not include the
"X-Parse-REST-API-Key" and "X-Parse-Application-Id"
How do I add them to the request ?
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
I try but get error on the first getrequest
java.lang.NullPointerException
B4X:
 Dim j As HttpJob
  j.Initialize("log-in", Me)
   j.GetRequest.SetHeader("X-Parse-REST-API-Key", "opBMoH2Qqguvx3eDhcwEAZ0Jdl4nuLUP5BgRZWZb")
  j.GetRequest.SetHeader("X-Parse-Application-Id", "vWcbP5PvajXyGcF123E0Z9F4tSCIwvS9AtIZmwCz")
  j.Download2("https://api.parse.com/1/login",Array As String( "[email protected]","kimel111"))

I also tried to initialize the getrequest:
B4X:
j.GetRequest.InitializeHead("https://api.parse.com/1/login")
This time there was no error but still the response is "unauthorized".
Please check with the parameters above - they are real.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
B4X:
  Dim j As HttpJob
  j.Initialize("log-in", Me)
  j.Download2("https://api.parse.com/1/login",Array As String( "[email protected]","kimel111"))
  j.GetRequest.InitializeHead("https://api.parse.com/1/login")
  j.GetRequest.SetHeader("X-Parse-REST-API-Key", "opBMoH2Qqguvx3eDhcwEAZ0Jdl4nuLUP5BgRZWZb")
  j.GetRequest.SetHeader("X-Parse-Application-Id", "vWcbP5PvajXyGcF123E0Z9F4tSCIwvS9AtIZmwCz")
'  j.GetRequest.SetContentType("application/json")

Program started.
java.lang.IllegalArgumentException: HTTP entity may not be null
at org.apache.http.util.EntityUtils.toByteArray(EntityUtils.java:68)
at anywheresoftware.b4a.http.HttpClientWrapper$ExecuteHelper.run(HttpClientWrapper.java:279)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
[jobname=log-in, success=false, username=
, password=, errormessage=Bad Request, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper@9e5e7e, tag=java.lang.Object@14dc149
, fx=anywheresoftware.b4j.objects.JFX@1ccc11d, httputils2service=null]

If I change the initializeHead to users then there is no response at all.

If I comment the initialization line I get:
Program started.
{"code":201,"error":"missing user password"}
[jobname=log-in, success=false, username=
, password=, errormessage=Bad Request, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper@1496198, tag=java.lang.Object@a663a4
, fx=anywheresoftware.b4j.objects.JFX@1d7c10a, httputils2service=null]
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Have you tried:
B4X:
Dim j As HttpJob
 j.Initialize("log-in", Me)
 j.Download2("https://api.parse.com/1/login",ArrayAsString( "[email protected]","kimel111"))
 j.GetRequest.SetHeader("X-Parse-REST-API-Key", "opBMoH2Qqguvx3eDhcwEAZ0Jdl4nuLUP5BgRZWZb")
 j.GetRequest.SetHeader("X-Parse-Application-Id", "vWcbP5PvajXyGcF123E0Z9F4tSCIwvS9AtIZmwCz")

?
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Program started.
{"code":201,"error":"missing user password"}
[jobname=log-in, success=false, username=
, password=, errormessage=Bad Request, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper@363e78, tag=java.lang.Object@13f668d
, fx=anywheresoftware.b4j.objects.JFX@165c301, httputils2service=null]

So I went to the url in the browser, put the user and password there and it also does not authorize me and wants to login again !
If I go to www.parse.com and there press login I get another screen and there the username and password are accepted and I log in.

Tried again now with this (users instead of login !):
B4X:
  Dim j As HttpJob
j.Initialize("log-in", Me)
j.Download2("https://api.parse.com/1/users",Array As String( "[email protected]","kimel111"))
j.GetRequest.SetHeader("X-Parse-REST-API-Key", "opBMoH2Qqguvx3eDhcwEAZ0Jdl4nuLUP5BgRZWZb")
j.GetRequest.SetHeader("X-Parse-Application-Id", "vWcbP5PvajXyGcF123E0Z9F4tSCIwvS9AtIZmwCz")

and Wallah - this time I got a successful login :):D:
Program started.
[jobname=log-in, success=true, username=
, password=, errormessage=, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper@c0d45, tag=java.lang.Object@13b5c39
, fx=anywheresoftware.b4j.objects.JFX@23b026, httputils2service=null]
{"results":[{"username":"[email protected]","createdAt":"2013-12-06T10:17:22.432Z","updatedAt":"2013-12-06T10:17:22.432Z","objectId":"6M9j4kFywq"}]}
 
Upvote 0
Top