Long lived access token lo longer working in python script - SOLVED

Struggling with long-lived access tokens, even if I had them working before. Context is a python script in an add-on.

Relevant part of below error message is HTTP Error 401: Unauthorized

Script started.
Traceback (most recent call last):
  File "zwave.py", line 288, in <module> zwave = ZWave()
  File "zwave.py", line 198, in __init__self._get_entities()
  File "zwave.py", line 228, in _get_entities response = urllib.request.urlopen(req)
  File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen return opener.open(url, data, timeout)
  File "/usr/lib/python3.6/urllib/request.py", line 532, in open response = meth(req, response)
  File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response 'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.6/urllib/request.py", line 570, in error return self._call_chain(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain result = func(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 650, in http_error_default raise TTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 401: Unauthorized

My addon parameter section looks like:

{
  "upd_interval": 1800,
  "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI1ZDRhMzkwNWRlMGM0N2Q2ODg4OGMzOGIzNjYxNzg5MiIsImlhdCI6MTU0MDEyMTQ4MiwiZXhwIjoxODU1NDgxNDgyfQ.DKMAPXdpOqobIiO_GtD5d-Q5zFdoFzOf1snYU22lw2A"
}

the code section where I perform the API call is:

def _get_entities(self):
    config_json = json.loads(open("/data/options.json").read())
    token = config_json["token"]
    url = "http://hassio/homeassistant/api/states"
    pwd = "Bearer " + token
    hdr = { 'Authorization' : pwd }
    req = urllib.request.Request(url, headers=hdr)
    response = urllib.request.urlopen(req)
    entities = json.load(response)
    response.close()

Python is not my strongest point - I have some doubt on double and single quotes and usually I manage to work things out after a few hit and miss attempts.

Strange thing is I had this working and then upgraded to 0.80 but when I downgraded to 0.78 again I couldn’t get it to work anymore. Super confused (?!)

If there is something obviously wrong a hint would be very much appreciated!

thx!

edit: access token deleted (of course).

Odd - this version is working:

    config_json = json.loads(open("/data/options.json").read())
    token = config_json["token"]
    ip = config_json["ip"]
    port = config_json["port"]
    pwd =  'Bearer ' + token
    hdr = { 'Authorization' : pwd }
    url = "http://" + ip + ":" + port + "/api/states"   <--  !!!
    req = urllib.request.Request(url, headers=hdr)
    response = urllib.request.urlopen(req)
    entities = json.load(response)

So it seems proxy: http://hassio/homeassistant/api/states is not working with token, however this link is working: “http://” + ip + “:” + port + “/api/states” with ip being actual local ip and not 127.0.0.1

Strange.