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:
 
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:

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: 325
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( "rderez@013net.net","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( "rderez@013net.net","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")


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

If I comment the initialization line I get:
 
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( "rderez@013net.net","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

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( "rderez@013net.net","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 :
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…