forked from singer-io/tap-xero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh_credentials.py
executable file
·48 lines (41 loc) · 1.31 KB
/
refresh_credentials.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
from xero.auth import PartnerCredentials
from xero.exceptions import XeroUnauthorized
import json
with open("config.json") as f:
config = json.loads(f.read())
def new_token():
credentials = PartnerCredentials(config["consumer_key"],
config["consumer_secret"],
config["rsa_key"])
print(credentials.url)
verifier = input("verifier: ")
credentials.verify(verifier)
return credentials
def refreshed_token():
credentials = PartnerCredentials(
config["consumer_key"],
config["consumer_secret"],
config["rsa_key"],
verified=True,
oauth_token=config["oauth_token"],
oauth_token_secret=config["oauth_token_secret"],
oauth_session_handle=config["oauth_session_handle"],
)
credentials.refresh()
return credentials
if "oauth_token" not in config:
credentials = new_token()
else:
try:
credentials = refreshed_token()
except XeroUnauthorized:
credentials = new_token()
state = credentials.state
for k in ["oauth_expires_at", "oauth_authorization_expires_at", "verified"]:
state.pop(k)
for k, v in state.items():
config[k] = v
with open("config.json", "w") as f:
f.write(json.dumps(config, indent=2))
f.write("\n")