-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteslatoken.py
More file actions
63 lines (53 loc) · 1.89 KB
/
Copy pathteslatoken.py
File metadata and controls
63 lines (53 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import logging
import os
import time
import requests
import yaml
import constants as C
from common import apicall
logging.basicConfig(
filename="powerwall.log",
level=logging.INFO,
format="%(asctime)s:%(levelname)s:%(message)s",
)
class Token:
NOW = time.time()
def __init__(self):
self.tokenstr = self.readtoken()
if self.tokenstr == None:
self.obtainToken()
self.tokenstr = self.readtoken()
def readtoken(self):
token = None
if os.path.isfile(C.TOKENFILE):
conf = yaml.safe_load(open(C.TOKENFILE))
expires = conf.get("expires_at", 0)
if self.NOW < expires:
token = conf.get("access_token")
return token
def obtainToken(self):
"""
This method makes an oauth Tesla API call to obtain an Access Token
"""
conf = yaml.safe_load(open(C.ACCOUNTS))
# url = C.BASE_URL + "/oauth/token"
auth = {
"grant_type": "password",
"client_id": C.TESLA_CLIENT_ID,
"client_secret": C.TESLA_CLIENT_SECRET,
"email": conf["data"]["email"],
"password": conf["data"]["password"],
}
logging.info("Getting new auth token...")
response = apicall(C.TOKEN_ENDPOINT, "POST", headers={}, data=auth)
# response = requests.post(url=url, data=auth, timeout=C.TIMEOUT)
if response is None or response.status_code != 200:
logging.error("Couldn't get auth token. Reason: %s" % (str(response)))
raise Exception("Couldn't get auth token. Reason: %s" % (str(response)))
expires = response.json().get("created_at", 0) + response.json().get(
"expires_in", 0
)
data = response.json()
data.update({"expires_at": expires})
with open(C.TOKENFILE, "w") as writer:
yaml.dump(data, writer)