B4J Question jOkHttpUtils2_NONUI library

codie01

Active Member
Licensed User
Longtime User
Hi All,

I am trying to use jOKhttpUtils2_NONUI from within an ABMaterial site to connect to SquarePos From terminal I can issue the following curl command correctly.

B4X:
curl -H "Authorization: Bearer xxxx-xxxxxxxxxxxxxxxxxxxx " https://connect.squareup.com/v2/locations

It returns location in json format.

However I have tried the following code in various combinations without success:

B4X:
    Dim j As HttpJob
    j.Initialize("j", Me)
    j.Head("https://connect.squareup.com/v2/")
    j.Head("Bearer xxxx-xxxxxxxxxxxxxxxxxxxx")
    j.Download2("https://connect.squareup.com/v2/",Array As String("Locations"))

I would appreciate some guidance please.

Kind Regards,
Phil

Current error returned in the log:

java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.IllegalArgumentException: unexpected url: Bearer xxxx-xxxxxxxxxx
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:119)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$1.run(WebSocketModule.java:126)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:191)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.IllegalArgumentException: unexpected url: Bearer xxxx-xxxxxxxxxx
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:491)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:437)
at b4j.example.itemsgenero._page_parseevent(itemsgenero.java:1009)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
... 10 more
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: unexpected url: Bearer xxxx-xxxxxxxxxx
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:119)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:482)
... 17 more
Caused by: java.lang.IllegalArgumentException: unexpected url: Bearer xxxx-xxxxxxxxxx
at okhttp3.Request$Builder.url(Request.java:142)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest.InitializeHead(OkHttpClientWrapper.java:398)
at b4j.example.httpjob._head(httpjob.java:205)
at b4j.example.itemsgenero._inventory5chip_clicked(itemsgenero.java:658)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
... 18 more
ResponseError. Reason: Not Found, Response:
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.Exception: Sub jobdone was not found.
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:491)
at anywheresoftware.b4a.keywords.Common.access$0(Common.java:462)
at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:536)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
 

DonManfred

Expert
Licensed User
Longtime User
j.Head("Bearer xxxx-xxxxxxxxxxxxxxxxxxxx")
i dont know what Head is... In B4A is must be

B4X:
    Dim job As HttpJob
    job.Initialize("getaccount",Me)
    job.PostString(mApiV2url&"users/get_account",$"{"account_id":"${accountID}"}"$)
    job.GetRequest.SetHeader("Authorization", "Bearer "&mAccessToken)

and i expect it should be the same in B4J

Make sure to set the Header AFTER the line
B4X:
j.Download2("https://connect.squareup.com/v2/",Array As String("Locations"))
 
Upvote 0

codie01

Active Member
Licensed User
Longtime User
Hi Don,

Thanks for your input, I learnt a lot, Sadly none worked. This however is close.

B4X:
    Dim myToken As String = "ABC123def456GHI789jkl0"
    Dim j As HttpJob
    j.Initialize("j", Me)
    j.Head("https://connect.squareup.com/v2/locations")
    j.GetRequest.SetHeader("Authorization", "Bearer " & myToken)
    Log(j.Download("https://connect.squareup.com/v2/locations"))

if I take out j.Head("https://connect.squareup.com/v2/locations") which is link as string, it does not work at all.

The error I get is:

ResponseError. Reason: Unauthorized, Response: {"errors":[{"category":"AUTHENTICATION_ERROR","code":"UNAUTHORIZED","detail":"Your request did not include an `Authorization` http header with an access token. The header value is expected to be of the format \"Bearer TOKEN\" (without quotation marks), where TOKEN is to be replaced with your access token (e.g. \"Bearer ABC123def456GHI789jkl0\")

The key is "Authorization" the value "Bearer & token"

Also It will return a json string, How would I handle this. The returned json structure is lie this:

{
"locations": [
{
"id": "9XX7D69CH0K4V",
"name": "02",
"address": {
"address_line_1": "Rose Bank",
"locality": "Cust",
"postal_code": "1740",
"country": "NZ"
},
"timezone": "America/Los_Angeles",
"status": "ACTIVE",
"created_at": "2018-02-28T22:25:58Z",
"merchant_id": "XXXXXXXXXXXXX",
"country": "NZ",
"language_code": "en",
"currency": "NZD",
"business_name": "generoSTUDIO",
"type": "PHYSICAL"
},
{
"id": "EPXKCYY7KKG22",
"name": "01",
"address": {
"address_line_1": "1347 Rosebank",
"locality": "Cust",
"postal_code": "1740",
"country": "NZ"
},
"timezone": "America/Los_Angeles",
"status": "ACTIVE",
"created_at": "2017-08-17T09:53:32Z",
"merchant_id": "XXXXXXXXXXXXX",
"country": "NZ",
"language_code": "en",
"currency": "NZD",
"business_name": "generoSTUDIO",
"type": "PHYSICAL"
},
{
"id": "KZHN4N3JTM9G6",
"name": "[email protected]",
"address": {
"country": "NZ"
},
"timezone": "Pacific/Auckland",
"status": "INACTIVE",
"created_at": "2017-08-17T00:56:02Z",
"merchant_id": "XXXXXXXXXXXX",
"country": "NZ",
"language_code": "en",
"currency": "NZD",
"business_name": "[email protected]",
"type": "PHYSICAL"
},
{
"id": "ZZ5SJS8XK83CW",
"name": "03",
"address": {
"address_line_1": "RoseBank",
"locality": "Cust",
"postal_code": "1740",
"country": "NZ"
},
"timezone": "America/Los_Angeles",
"status": "ACTIVE",
"created_at": "2018-02-28T22:26:23Z",
"merchant_id": "XXXXXXXXXXXXX",
"country": "NZ",
"language_code": "en",
"currency": "NZD",
"business_name": "generoSTUDIO",
"type": "PHYSICAL"
}
]
}

Is there anywhere I can read a tutorial?

Thanks Phil
 
Upvote 0

codie01

Active Member
Licensed User
Longtime User
Hi Don,

I guess its this line.

job.PostString(mApiV2url&"users/get_account",$"{"account_id":"${accountID}"}"$)

But I don't understand it :(

Thanks Phil
 
Upvote 0

codie01

Active Member
Licensed User
Longtime User
Don, It worked, Thank you, Thank you, I really appreciate your help, Thanks! Now I just need to work out how to read the json string in JobDone :)
 
Upvote 0

codie01

Active Member
Licensed User
Longtime User
Don do you have a paypal Account, your donate button does not wor from my Country, You have helped me a lot lately and I want to pay something, Thanks.
 
Upvote 0

codie01

Active Member
Licensed User
Longtime User
Hi Don,

I have all the download API calls working, Thanks. Now I'm working on the upload API's

I have a json string called "jItems" do i use the following code, and also I need to set the content_type, how do I do this.

B4X:
j.PostString("https://connect.squareup.com/v1/XXXXXXXXXXXXX/items",jItems)

Thanks
 
Upvote 0
Top