Jump to content


HTTP response 401 with Python 3

  • Please log in to reply
No replies to this topic

#1 zacrify



  • Members
  • Pip
  • 1 posts

Posted 02 January 2019 - 04:30 AM

Hi I'm really new for programming and just want to start a altair water consumption project from here 

I use code from 9. topic and found it was python 2 code.

since I use Python3.5.3, I tried to manipulate many ways and end up got stuck with HTTP response 401


this is my code


#!/usr/bin/env python
import serial
from urllib.request import urlopen, Request
import urllib.parse
import json

# Serial port connection
  ser = serial.Serial("/dev/ttyUSB0", 9600, timeout=1)
  ser = serial.Serial("/dev/ttyS0", 9600, timeout=1)

pulses = 0;
liters = 0;
APIKEY="XXXXXXXX" #Replace with your APIKEY 
DEVICE = "defaultDevice@YYYYYY.YYYYYY" #Replace with the id_developer of your devices
class Client (object):
  api_url = "http://api.altairsmartcore.com/streams"
  def __init__ (self, api_key = None, client_type = json):
                self.client_type = client_type
                self.api_key = api_key
                self.content_type = "application/vnd.altairsc.api.v2+%s" % (self.client_type)
                self.headers = {'User-Agent': 'Raspberry-SmartCore',
                  'Content-Type': self.content_type,
                  'Accept': self.content_type,
                  'Apikey': self.api_key}

  def send (self, data):
                self.data = urllib.parse.urlencode(data).encode("utf-8")
                #self.data = json.dumps(data)
                request = Request(Client.api_url, self.data, self.headers)
                self.response = urlopen(request)
                return self.response

client_smartcore = Client (APIKEY)

while True:
        line = ser.readline().strip()
        x = json.dumps(line.decode('utf-8'))
        if not(line is b''):
                print (line)
                data = {"protocol":"v2","device":DEVICE,"at":"now","data":{"consumption":line}}
                smartcore_response = client_smartcore.send(data)
                print (smartcore_response.read())
#if line:
# print (line)  
  # Remove possible garbage lines
# if line.startswith('Litres'):
#   piece = line.split(":")
#   if 2==len(piece):
#     liters = int(piece[1].strip())
#     print (liters)
#     data = {"protocol":"v2","device":DEVICE,"at":"now","data":{"consumption":round(liters, 3)}}
#     smartcore_response=client_smartcore.send(data)
#     print (smartcore_response.read())


this is an error i got when compile code.


Traceback (most recent call last):
  File "/home/pi/altair1.py", line 43, in <module>
    smartcore_response = client_smartcore.send(data)
  File "/home/pi/altair1.py", line 32, in send
    self.response = urlopen(request)
  File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 472, in open
    response = meth(req, response)
  File "/usr/lib/python3.5/urllib/request.py", line 582, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.5/urllib/request.py", line 510, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 590, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized

Could anyone help me with this?


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users