I need help with Authorization using the API URL on the current beta version of Domoticz. I have spent lots of hours with that but I just do not succeed in get authorized.
The quite simple Python script I try to get run is:
Code: Select all
#!/usr/bin/python
import sys
import urllib
import urllib2
import base64
sensor_idx = 16
url = "http://192.168.178.32:8080"
url_json = url + "/json.htm"
cpuTemp = int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1000.0
cmd = url_json + "?" + "type=command¶m=udevice&idx=" + str(sensor_idx) + "&nvalue=0&svalue=" + str(cpuTemp)
print cmd
base64string = base64.encodestring('%s:%s' % ("LOGIN", "PASSWORD")).replace('\n', '')
request = urllib2.Request(cmd)
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib2.urlopen(request)
print response.read()
The user LOGIN is added on "Setup" -> "More options" -> "Edit users" and has admin rights on all devices.
In "Setup" -> "Settings" I have tried out a lot of settings. Allowed everything. Entered different credentials and cleared them completely. Added 192.168.178.32 and 127.0.0.1 (and submasks) to "Local Networks (no username/password):"
But nothing helps! When i type on SSH:
Code: Select all
sudo python cpu_temperature.py
Code: Select all
http://192.168.178.32:8080/json.htm?type=command¶m=udevice&idx=1&nvalue=0&svalue=48
Traceback (most recent call last):
File "cpu_temperature.py", line 33, in <module>
response = urllib2.urlopen(request)
File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 435, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 548, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 473, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 556, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized
When I type the link (http://192.168.178.32:8080/json.htm?typ ... &svalue=48) in most cases everything works fine. But sometimes I get "401 Unauthorized" too.
I have tried to delete and install Domoticz again. I have not changed any settings after the installation. And it worked! With SSH and as crontab. But only till a restart of Raspberry Pi 3. Afterwards it is the same: "401 Unauthorized"...
I would be very grateful for every hint.