-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_device_code_flow.py
41 lines (32 loc) · 1.19 KB
/
client_device_code_flow.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
from datetime import datetime
import time
import webbrowser
import httpx
from client_refresh_flow import RefreshFlow
def login(client):
info = client.post("/authorize").json()
print(f"""You have {info['expires_in']} seconds to enter the code
{info['user_code']}
after authorizing at the URL:
{info['authorization_uri']}
""")
webbrowser.open(info["authorization_uri"]) # Try to open a web browser.
deadline = datetime.now().timestamp() + info["expires_in"]
# Poll the /token endpoing waiting for this pending session to be verified.
print("Waiting...", end="", flush=True)
while datetime.now().timestamp() < deadline:
response = client.post("/token", data={"device_code": info["device_code"]})
print(".", end="", flush=True)
if response.status_code == 200:
print("\nLogged in!")
break
time.sleep(info["interval"])
tokens = response.json()
client.auth = RefreshFlow(tokens, f"{client.base_url}/refresh")
return client
if __name__ == "__main__":
import time
client = httpx.Client(base_url="http://localhost:8000")
login(client)
print(client.get("/data"))
print(client.get("/data").json())