API login

Primtek7 years ago

Traccar API login uses basicAuth ..
I use C# codes below, why it always return OK although I use wrong password?

string url = "http://xxxxxxxxxxxxxxxxxxxxx:8082"; 
var byteArray = Encoding.ASCII.GetBytes(email + ":" + password);

var client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

var response = client.GetAsync(url).Result;
HttpStatusCode statusCode = response.StatusCode;
Primtek7 years ago

please ignore this.. resolved! thanks

Anton Tananaev7 years ago

Again, it seems like you haven't read documentation before asking question.

Primtek7 years ago

Anton

No, thats my C# problem, not related to Traccar

Anton Tananaev7 years ago

One of the problems that I see is that you are not using correct API endpoint, which is documented.

Primtek7 years ago

What do you mean by connect using IP Point?

What I found in the doc is:
——-
Security
basicAuth
Type: basic
Description:
Basic HTTP authorization with email and password
——

Do you mean the port?
I still able to connect to 8082 and 5055 and I still checking this.

Anton Tananaev7 years ago

I mean URL, which is completely incorrect.

Primtek7 years ago

noted and thanks Anton..

my latest try below has succeed to create session, but what I don't understand why the "Token" is empty?, as I read from the doc there is no token parameter required to access the API functions.. or I still missed something? .. (

string url = "http://mydomain.com:8082/api/session";

var pairs = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("email", email),
    new KeyValuePair<string, string>("password", password)
};

var client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "x-www-form-urlencoded");

var content = new FormUrlEncodedContent(pairs);
var response = client.PostAsync(url, content).Result;
HttpStatusCode statusCode = response.StatusCode;
switch (statusCode)
{
    case HttpStatusCode.OK:
        {
            response.EnsureSuccessStatusCode();
            string res = response.Content.ReadAsStringAsync().Result;
            if (response.IsSuccessStatusCode)
            {
                JObject objGPS = JObject.Parse(res);
                if (objGPS.HasValues)
                {
                    User_Id = (int)objGPS["id"];
                    User_Token = (string)objGPS["token"];   ---> token empty
                }
            }
        }
Anton Tananaev7 years ago

It's empty because you haven't set it.

Obaid Khan7 years ago

@PrimTek Can you please share your C# code. I am trying to use cookies in c# since quite a long time but still failed.

Obaid Khan7 years ago

@Anton Tananaev how do I use token to get list of devices. I see no option in C# library generated through swagger.

List<Device> DevicesGet (bool? all, int? userId, int? id, string uniqueId);

This is the method in DefaultApi class. I see no option for token here.

Anton Tananaev7 years ago

You need to create a session using token and then use that session to issue other requests (e.g. get device list).

Obaid Khan7 years ago

How do I do that in C# and Android?

Obaid Khan7 years ago

How to get devices using basic auth?
Please provide a sample code snippet.

Anton Tananaev7 years ago

I don't have a sample, but I'm sure there are plenty of samples online on how to use basic auth. Probably you can find some on StackOverflow.