-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetRefreshToken.py
33 lines (27 loc) · 927 Bytes
/
GetRefreshToken.py
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
import json
import requests
secrets_file = "secrets.json"
with open(secrets_file) as f:
secrets_data = json.load(f)
api_key = secrets_data["API_KEY"]
access_token = secrets_data["ACCESS_TOKEN"]
refresh_token = secrets_data["REFRESH_TOKEN"]
def getRefreshToken(refresh_token, api_key):
url = "http://api.ecobee.com/token"
payload = {
"grant_type": "refresh_token",
"code": refresh_token,
"client_id": api_key,
}
response = requests.post(url, data=payload)
if response.status_code == 200:
return response.json()
else:
print("Error:", response)
return None
token_response = getRefreshToken(refresh_token, api_key)
new_access_token = token_response["access_token"]
secrets_data["ACCESS_TOKEN"]=new_access_token
with open(secrets_file, 'w') as file:
json.dump(secrets_data, file, indent=4)
print("Access Token was successfully refreshed")