Logic Machine Forum
HTTP GET with authorisation - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: HTTP GET with authorisation (/showthread.php?tid=444)



HTTP GET with authorisation - buuuudzik - 29.10.2016

Hello,

I want integrate with Traccer server. This server give the JSON when I log in and brows such address:
http://demo.traccar.org/api/positions/


But for LM communication I must have some authorisation. What should I add to this script?

Code:
-- load required modules
http = require("socket.http")
mime = require("mime")

-- Alternatively, one could fill the appropriate header and authenticate
-- the request directly.
r, c, d, e = http.request {
  url = "http://demo.traccar.org/api/positions/",
  headers = { authorization = "Basic " .. (mime.b64("login:password")) }
}

log(r,c,d,e)
Now it gives:

Code:
* arg: 1
 * number: 1
* arg: 2
 * number: 200
* arg: 3
 * table:
  [server]
   * string: Jetty(9.2.19.v20160908)
  [content-type]
   * string: application/json
  [connection]
   * string: close
  [content-length]
   * string: 393
  [access-control-allow-headers]
   * string: origin, content-type, accept, authorization
  [access-control-allow-origin]
   * string: *
  [date]
   * string: Sat, 29 Oct 2016 22:39:46 GMT
  [access-control-allow-credentials]
   * string: true
  [access-control-allow-methods]
   * string: GET, POST, PUT, DELETE, OPTIONS
* arg: 4
 * string: HTTP/1.1 200 OK

[b]Traccar[/b] 30.10.2016 00:39:27
* arg: 1
 * number: 1
* arg: 2
 * number: 200
* arg: 3
 * table:
  [server]
   * string: Jetty(9.2.19.v20160908)
  [content-type]
   * string: application/json
  [connection]
   * string: close
  [content-length]
   * string: 393
  [access-control-allow-headers]
   * string: origin, content-type, accept, authorization
  [access-control-allow-origin]
   * string: *
  [date]
   * string: Sat, 29 Oct 2016 22:39:27 GMT
  [access-control-allow-credentials]
   * string: true
  [access-control-allow-methods]
   * string: GET, POST, PUT, DELETE, OPTIONS
* arg: 4
 * string: HTTP/1.1 200 OK

Login is an email so it has '@'.


RE: HTTP GET with authorisation - admin - 31.10.2016

200 status means that requests work correctly, you just have to get the response like this:
Code:
-- load required modules
http = require("socket.http")
mime = require("mime")
ltn12 = require("ltn12")

t = {}

r, c, d, e = http.request {
  url = "http://demo.traccar.org/api/positions/",
  headers = { authorization = "Basic " .. (mime.b64("login:password")) },
  sink = ltn12.sink.table(t)
}

response = table.concat(t)
log(response)



RE: HTTP GET with authorisation - buuuudzik - 31.10.2016

It works perfectlyWink


RE: HTTP GET with authorisation - Domoticatorino - 04.11.2016

Hi Buuuuudzik,
the first code is saved as user script? What about the second script?
How is it managed?
Thanks.

Do you use it for geofencis? Because it seems Traccar does not support geofencis.
Thank you for your help.
Regards.


RE: HTTP GET with authorisation - buuuudzik - 04.11.2016

(04.11.2016, 17:27)Domoticatorino Wrote: Hi Buuuuudzik,
the first code is saved as user script? What about the second script?
How is it managed?
Thanks.

Do you use it for geofencis? Because it seems Traccar does not support geofencis.
Thank you for your help.
Regards.

Traccar has 2 applications:
- server (you can use demo server or you can install server on your pc) or you can use LM as a server for clients,
- clients (iOS, Android etc.) (you can set interval for sending data to server)

This is the example respond from the server:
* string: [{"id":123141121,"attributes":{"battery":"0","ip":"37.47.0.6","distance":0.0,"totalDistance":4.865613311E7},"deviceId":34294,"type":null,"protocol":"osmand","serverTime":null,"deviceTime":"2016-10-30T08:05:35.000+0000","fixTime":"2016-10-30T08:05:35.000+0000","outdated":false,"valid":true,"latitude":50.98288,"longitude":23.170059,"altitude":185.619,"speed":0.0,"course":0.0,"address":null}]

You have this data from server and for example you can use it for detecting how far some person is from home etc. For Geofencing you must use additionally some other server which converts latitude and longtitude into e.g. city.


RE: HTTP GET with authorisation - Domoticatorino - 05.11.2016

Thank you.
But what is the procedure to install the server on LM?
Please consider that I am using Homelynk by Schneider.
Thank you?


RE: HTTP GET with authorisation - edgars - 15.11.2016

Here is a ready example for Geolocalization with LogicMachine based on Traccar service:
http://openrb.com/geolocalization-with-lm-based-on-traccar-service

Thanks to buuuudzik for this nice step-by-step guide! Smile


RE: HTTP GET with authorisation - Domoticatorino - 06.12.2016

Hi Edgards,
Does it work with iPhone only?


RE: HTTP GET with authorisation - buuuudzik - 07.12.2016

No, it works on iphone and android and on a lot of professional gps trackers.

Look here for information about mobile clients:
https://www.traccar.org/client/

List of GPS tracking devices and applications supported by Traccar server:
https://www.traccar.org/devices/


RE: HTTP GET with authorisation - gjniewenhuijse - 07.12.2016

battery drain is very high on iphones