API Login and request for devices

hmax9 years ago

I tried to access Traccar-Data using the API, but having problems with the authentication (Login).
My code is as follows:

   Function TraccarAPI_GetDevices(ByVal sUser As String, ByVal sPassword As String) As String
        Dim client As New WebClient()

        Dim response As Byte() = client.UploadValues("http://xxx.xxx.xxx.xxx:8082/api/login", New NameValueCollection() From { _
  {"email", sUser}, _
  {"password", sPassword} _
 })
        TextBox1.Text = System.Text.Encoding.UTF8.GetString(response)

        response = client.UploadValues("http://xxx.xxx.xxx.xxx:8082/api/device/get", New NameValueCollection() From { _
  {"", ""} _
 })

        TextBox1.Text = TextBox1.Text & vbCrLf & System.Text.Encoding.UTF8.GetString(response)

        Return System.Text.Encoding.UTF8.GetString(response)
    End Function

The Result is:

{"success":true,"data":{"name":"admin","id":1,"longitude":0.0,"latitude":0.0,"email":"admin","admin":true,"zoom":0}}
{"success":false,"error":"User not logged in"}

So, it's strange - login returns "true" but the request for the devices returns, that I'm not logged in.
Just to mention: for now, I'm using the default username and password (admin).

Any ideas?

Markus

Anton Tananaev9 years ago

You need to keep the same session. I guess you are not persisting the session across requests that's why it fails.

hmax9 years ago

You are right! I just had to switch to a customized WebClient-Class which supports Cockies as mentioned in this article: http://stackoverflow.com/questions/3962825/in-net-how-do-you-provide-a-session-identifier-to-a-web-service-that-requires

Hi Hmax
I tried using your code to connect to Traccar, but getting a 404 error.
Code:

       WebClient client = new WebClient();
       NameValueCollection nvc = new NameValueCollection();
       nvc.Add("email", txtUserName.Text);
       nvc.Add("password", txtPassword.Text);
       byte[] responseArray = client.UploadValues("http://demo2.traccar.org/api/login", nvc);
Primtek6 years ago

Hi.. I am facing the same problem.. did you resolve this?