Authorization Problems

Python and python framework

Moderator: leecollings

Post Reply
Ghost
Posts: 1
Joined: Saturday 28 October 2017 12:57
Target OS: -
Domoticz version:
Contact:

Authorization Problems

Post by Ghost »

Hello Friends,

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&param=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()
192.168.178.32 is the IP of the Raspberry Pi 3 on which Domoticz is running.

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
I get:

Code: Select all

http://192.168.178.32:8080/json.htm?type=command&param=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
The other way of authorization according to Wiki (with URL containing encoded User and Password) produces the same result.

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.
kofec
Posts: 53
Joined: Friday 16 September 2016 14:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Authorization Problems

Post by kofec »

I have the same problem
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Authorization Problems

Post by emme »

have you tried:
url = "http://user:[email protected]:8080"

or enter the ip address in the whitelist unser settings?

if the script runs on the same hardware of domoticz try using 127.0.0.1 as ip address
The most dangerous phrase in any language is:
"We always done this way"
Geko
Posts: 1
Joined: Thursday 22 November 2018 10:17
Target OS: -
Domoticz version:
Contact:

Re: Authorization Problems

Post by Geko »

I have the same problem.
Even if i directly type the [url]http://username:password@ip:port>/json.htm?api-call[/url] to the browser I get an Err back....
Is this a bug?

Andy
dobber
Posts: 19
Joined: Saturday 07 May 2016 1:43
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta..
Location: Sweden
Contact:

Re: Authorization Problems

Post by dobber »

I'm seeing the same thing now. json api sending unauthorized until I go in to settings and press "save". After this api works great until next reboot.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest