lephisto / tesla-apiscraper

API Scraper for pulling Vehicle Statistics from the Tesla Owner API into an InfluxDB + Grafana Dashboards
GNU Lesser General Public License v3.0
364 stars 102 forks source link

401 unauthorized #38

Open evanrich opened 5 years ago

evanrich commented 5 years ago

Tried running your docker example, verbatim (didn't change anything),, and whne it starts up exits with a 401 unauthorized:

Traceback (most recent call last):
  File "/tesla-apiscraper/apiscraper.py", line 319, in <module>
    state_monitor = StateMonitor(a_tesla_email, a_tesla_passwd)
  File "/tesla-apiscraper/apiscraper.py", line 83, in __init__
    self.connection = teslajson.Connection(a_tesla_email, a_tesla_passwd)
  File "/tesla-apiscraper/teslajson.py", line 76, in __init__
    self.vehicles = [Vehicle(v, self) for v in self.get('vehicles')['response']]
  File "/tesla-apiscraper/teslajson.py", line 83, in get
    return self.post(command, None)
  File "/tesla-apiscraper/teslajson.py", line 89, in post
    auth = self.__open("/oauth/token", data=self.oauth)
  File "/tesla-apiscraper/teslajson.py", line 126, in __open
    resp = opener.open(req)
  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

If I use POSTMAN to test this, it works...not sure what it is in your code, but something's off: image

I left part of my password in in case that might be the issue has & and % in it). from what I can tell, the url you query is correct, and the client secret /ID match what i've found in other examples for tesla api.. only thing i can think of is either the password isn't being parsed right or something else.

Edit: In postman, when i took out "content-type: application/json", i got a 401 in postman. looking at teslajson.py, i dont see any headers being sent on the get?

lephisto commented 5 years ago

I cannot tell for the older version, i just adapted the whole Dockerfile now, and it works fine for me. ofcoz you need the application/json request header, the python modules cares for it. Is your Docker container maybe connecting through a proxyserver?

lephisto commented 5 years ago

Try the new dockercompose. It should be better.

evanrich commented 5 years ago

I haven't tried the compose yet, just tried building the docker container as-is so i could toss it in my kubernetes cluster. Literally did the following steps:

1.) git clone... 2.) docker build -t teslaapi:latest . 3.) docker run -p 3000:3000 --name teslaapi3 -e "TESLA_USERNAME=" -e "TESLA_PASSWORD=" teslaapi:latest

Get the following output:

Starting influxdb...
influxdb process was started [ OK ]
Starting Grafana Server: failed!
Failed to initialize Owner API

going straight to the internet, no proxy. I am able to resolve, although not ping the owner-api.teslamotors.com hostname so DNS isn't an issue:

ping owner-api.teslamotors.com
PING owner-api.vn.tesla.services (205.234.31.120) 56(84) bytes of data.

So I went and changed my password, from the 40+ character one I had (with "%" in it), to some 16 character one without "%" in it. Ran the container again, it now works. I don't have a lot of time to debug now, but I'm guessing maybe "%" isn't being parsed correctly in your code. I'll try to debug later this week but for now, it works without a very long password + special characters.

2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...dr: old value: , new value: 0
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...ft: old value: , new value: 0
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...is_user_present: old value: , new value: False
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...locked: old value: , new value: True
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...media_state: old value: , new value: {'remote_control_enabled': True}
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...notifications_supported: old value: , new value: True
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...odometer: old value: , new value: 55715.782341
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...parsed_calendar_supported: old value: , new value: True
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...pf: old value: , new value: 0
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...pr: old value: , new value: 0
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...remote_start: old value: , new value: False
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...remote_start_supported: old value: , new value: True
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...rt: old value: , new value: 0
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...software_update: old value: , new value: {'expected_duration_sec': 2700, 'status': ''}
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...speed_limit_mode: old value: , new value: {'min_limit_mph': 55, 'active': False, 'max_limit_mph': 90, 'current_limit_mph': 56.0, 'pin_code_set': True}
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...sun_roof_percent_open: old value: , new value: 0
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...sun_roof_state: old value: , new value: unknown
2019-04-16 03:31:01 DEBUG    Value Change, SG: vehicle_state: Logging...valet_mode: old value: , new value: False
2019-04-16 03:31:01 INFO     Writing Points to Influx: vehicle_state
2019-04-16 03:31:01 INFO     Asleep since: 0 Sleeping for 1 seconds..
2019-04-16 03:31:02 DEBUG    Car State: online Poll Interval: 1
2019-04-16 03:31:02 DEBUG    >> Request vehicle data
2019-04-16 03:31:03 DEBUG    Only minimal range difference received. No change registered to avoid wakelock.
2019-04-16 03:31:03 DEBUG    Only minimal range difference received. No change registered to avoid wakelock.
2019-04-16 03:31:03 DEBUG    Only minimal range difference received. No change registered to avoid wakelock.
2019-04-16 03:31:03 DEBUG    starting thread elevator:  <redacted>
Lockfile detected, skipping
2019-04-16 03:31:03 INFO     Asleep since: 0 Sleeping for 2 seconds..
2019-04-16 03:31:04 DEBUG    Elevation: 27
DEBUG:apiscraper:Elevation: 27
2019-04-16 03:31:05 DEBUG    Car State: online Poll Interval: 2
DEBUG:apiscraper:Car State: online Poll Interval: 2
notnicehat commented 5 years ago

Just a guess - lines 72 and 73 of the Dockerfile use a sed one-liner to substitute the environment variables of TESLA_USERNAME and TESLA_PASSWORD into the config.py file and it's likely % or otherwise reserved characters is the problem.

So I went and changed my password, from the 40+ character one I had (with "%" in it), to some 16 character one without "%" in it. Ran the container again, it now works. I don't have a lot of time to debug now, but I'm guessing maybe "%" isn't being parsed correctly in your code. I'll try to debug later this week but for now, it works without a very long password + special characters.

bcrooker commented 5 years ago

I am running into a similar issue, originally I did have symbols in my password, but even after changing my password to be only alpha numeric characters I get the same error.

OleTheill commented 3 years ago

I am getting the "failed to initialize Owner API" here too. I just finished installing, so it could be a setup-problem. However, I know 100% sure that: