B4J Question ODataV4: I can't connect to server with Windows / NTLM

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
I'm trying to get information from Businnes Central on premise.
Using Browser, Excel or Power BI I can connect and obtain data without problem, but I can't doing using B4J.
This is the code I'm using:

Code Example:
Private Sub Button1_Click
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Username = "xxx"
    j.Password = "yyy"
    j.Download("http://192.168.0.10:19058/BC190_DD/ODataV4/Company('DB_NAME')/LocationListPage")
    j.GetRequest.Timeout = 3000 * 1000
    j.GetRequest.SetHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8")
    'j.GetRequest.SetHeader("Authorization", "Basic xxxx yyyy ")
    j.GetRequest.SetHeader("Authorization", "Basic ")
        Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release

And this is the response:

Response::
ResponseError. Reason: Unauthorized, Response:

Could anybody help me?

Thanks.
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Upvote 0

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
you sure this is how you build the auth header? usually the basic is a B64 encoded string


i am not sure this characters are valid in a URL is really that the endpoint?
The code now is::
Private Sub Button1_Click
    Dim SU As StringUtils
    Dim usr, pass As String
    usr = "Aladdin"
    pass = "open sesame"
    Dim s As String  = usr & ":" & pass
    Dim credentials As String  = SU.EncodeBase64(s.GetBytes("UTF8"))
    Log(credentials)
    
    Dim b() As Byte = SU.DecodeBase64(credentials)
    Dim d As String = BytesToString(b, 0, b.Length, "UTF8")
    Log(d)
    
    Dim j As HttpJob
    j.Initialize("", Me)
    'j.Username = "xxx"
    'j.Password = "yyy"
    j.Download("http://192.168.0.10:19058/BC190_DD/ODataV4/Company('LA%20COOPERATIVA')/LocationListPage")
    'j.GetRequest.Timeout = 3000 * 1000
    'j.GetRequest.SetHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8")
    'j.GetRequest.SetContentType("plain/text")
    j.GetRequest.SetHeader("Authorization", "Basic " & credentials)
        Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release
End Sub

But the result is the same: ResponseError. Reason: Unauthorized, Response:
 
Upvote 0

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
I have continued testing the connection to the ODATA data source and in the end I succeeded using Excel.
Attached is a screenshot of how I had to configure the connection to be able to log in and obtain the data correctly.
It seems that instead of the "Basic" option I should do it using the "Windows" option, being able to enter the domain/user and password with which to connect....
 

Attachments

  • capturaOdata.png
    capturaOdata.png
    18 KB · Views: 113
Upvote 0

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
Hello again.
I have continued testing and if I try to make the connection using the NTLM method for Authorization, the error message changes.
Will it be a good sign to continue investigating or do I give up?
Thank you.

B4X:
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("http://192.168.0.10:19058/BC190_DD/ODataV4/LocationListPage")
    j.GetRequest.Timeout = 3000 * 1000
    j.GetRequest.SetHeader("Accept", "application/json")
    j.GetRequest.SetHeader("Authorization", "NTLM " & credentials)
    Wait For (j) JobDone(j As HttpJob)

Y ahora lo que da es:
B4X:
ResponseError. Reason: Bad Request, Response:

Error 400
 
Upvote 0

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
Previus instructions:
To connect to an OData API with Windows user authentication in a B4J application, you can use the jODataClient library, which is a B4J-compatible OData client library capable of handling Windows user authentication. Here is an example of how to do it:

Add the jODataClient library:

Download the jODataClient library from the B4X forum: https://www.b4x.com/android/forum/threads/jodataclient-v2-0.90197/
In your B4J project, go to the "Libraries" menu and select "Add External Jar". Next, select the .jar file you downloaded from jODataClient.

Write the code for the OData request:
Chat GPT Suggestion for B4J:
import jODataClient.Main;
import jODataClient.Entities;
import jODataClient.CredentialCache;

Sub AppStart(Form1 As Form, Args() As String)
    ' URL de la API OData
    Dim odataURL As String
    odataURL = "https://tu-api-odata.com/endpoint"

    ' Crea una instancia de Main
    Dim jOData As Main
    jOData.Initialize

    ' Crea una instancia de CredentialCache para la autenticación de Windows
    Dim credentials As CredentialCache
    credentials.Initialize

    ' Agrega las credenciales de Windows al caché
    credentials.AddWindowsCredentials("usuario", "contraseña")

    ' Establece las credenciales en la instancia de Main
    jOData.CredentialCache = credentials

    ' Realiza la solicitud OData
    Dim entities As Entities
    entities = jOData.ExecuteQuery(odataURL, "Entities")

    ' Procesa la respuesta OData
    If jOData.IsSuccess Then
        Dim result As String
        result = entities.ToString
        Log(result)
    Else
        Log("Error en la solicitud OData: " & jOData.ErrorMessage)
    End If
End Sub
 
Upvote 0

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
The windows credentials doesn't use the rest authorization protocol. It seems you have to setup that specific user with a different auth method
I found the solution in this thread: https://www.b4x.com/android/forum/t...entication-ntlmv2-solution.66790/#post-424367

In my case, I only had to change:
B4X:
Original:
    hj.PostString("http://192.168.0.10:19058/BC190_DD/ODataV4/",tp)
changed to
    hj.Download("http://192.168.0.10:19058/BC190_DD/ODataV4/")
and added
    hj.GetRequest.Timeout = 5 * 60 * 1000
 
Last edited:
Upvote 0
Top