B4J Question Consume Web Service

makis_best

Well-Known Member
Licensed User
Longtime User
Hi

I create a B4J application and in one part of it I need to access PrestaShop web services.
I look in the forum for 4-5 days trying to figure out witch libraries I need to use or how to do.
All the information are very confusing.
Is there any example using authentication and accessing a service similar with PrestaShop?
Or someone explain to me the steps and libraries needed?
I already watch @Erel video about web services about Android is the same for B4J?

Thank you.
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
Based on information from https://devdocs.prestashop.com/1.7/development/webservice/, something like
B4X:
'That's the access token used in the above documentation. Looks like it is
' already Base64 encoded and ready to be used in an authentication header
Dim accessToken As String = "UCCLLQ9N2ARSHWCXLT74KUKSSK34BFKX"
Dim j As HttpJob
j.Initialize("", Me)
'I'm using https, but the site you're using may not support it (hopefully it does).
' Domain name needs to be whatever you are using and then you may also have
' a subfolder. See above linked docs for more info
j.Download("https://example.com/api/")
'Using the access token as is, since it looks like it is Base64 encoded
' (and the sample in the documentation does it so)
j.GetRequest.SetHeader("Authorization", $"Basic ${accessToken}"$)
'If you don't set the Io-Format to JSON, you will have to deal with XML documents
j.GetRequest.SetHeader("Io-Format", "JSON")
Wait For (j) JobDone(j As HttpJob)
If j.Success = True Then
   'According to the docs, with what we are doing here, you should be getting
   ' "a summary of the available APIs you can call with your access token"
   Log(j.GetString)
Else
   Log(j.ErrorMessage)
End If
j.Release
Note: untested
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You need your own access token. The access token is just the one from the API documentation. I guess I could have clarified that more.
 
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
I use My token...

I make some changes and I get the result I wanted...

The new code is...

B4X:
Sub ReadClientTable
    Dim Job As HttpJob
    Job.Initialize("j1", Me)
    Job.Username = PresUserName
    Job.Download("https://website.xy/api/customers?ws_key=" & PresUserName)
    Job.GetRequest.SetHeader("Authorization", $"Basic ${PresUserName}"$)
    Job.GetRequest.SetHeader("Io-Format", "JSON")
End Sub

Sub JobDone(job As HttpJob)
    If job.Success Then
        Log(job.GetString)
    End If
    job.Release
End Sub

Thank you so much for your help.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
B4X:
'Make sure you add a colon on the end of your access token!!!!!!
Dim accessToken As String = "UCCLLQ9N2ARSHWCXLT74KUKSSK34BFKX:"
Dim su As StringUtils ' requires StringUtils library to be checked
Dim authInfo As String = su.EncodeBase64(accessToken.GetBytes("UTF8"))
Dim j As HttpJob
j.Initialize("", Me)
'I'm using https, but the site you're using may not support it (hopefully it does).
' Domain name needs to be whatever you are using and then you may also have
' a subfolder. See above linked docs for more info
j.Download("https://example.com/api/")
'Pass along authInfo for authorization (Base64 encoded version of modified access token above)
j.GetRequest.SetHeader("Authorization", $"Basic ${authInfo}"$)
'If you don't set the Io-Format to JSON, you will have to deal with XML documents
j.GetRequest.SetHeader("Io-Format", "JSON")
Wait For (j) JobDone(j As HttpJob)
If j.Success = True Then
   'According to the docs, with what we are doing here, you should be getting
   ' "a summary of the available APIs you can call with your access token"
   Log(j.GetString)
Else
   Log(j.ErrorMessage)
End If
j.Release
I know you solved it, but try this. I looked on the forum, and the code for Presta uses standard PHP to get the authorization header. Therefore token needs to be base 64 encoded (with colon appended). This way you are not leaving you API key public by including it in the URL (many servers log URL's). Also, you may be comfortable using JobDone as you do, but try to switch to Wait For (you may actually get more help that way, especially if you post your code).
 
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
Dear @OliverA your code run perfect.
The JSON string return correctly ready to Parse. ;)
Thank you once more for your help.
 
Last edited:
Upvote 0
Top