-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.py
executable file
·55 lines (44 loc) · 1.33 KB
/
auth.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
49
50
51
52
53
54
55
#!/usr/bin/python3
import http.server
import json
import os.path
from server import UploadToGoogleDriveServer
script_path = os.path.abspath(__file__)
script_dir = os.path.dirname(script_path)
def read_client_secret():
client_name = 'client_secret.json'
client_path = os.path.join(script_dir, client_name)
with open(client_path, 'r') as f:
client = json.load(f)
assert client is not None and 'installed' in client
client = client['installed']
return client
def write_refresh_token(refresh_token):
refresh_name = 'refresh_token.txt'
refresh_path = os.path.join(script_dir, refresh_name)
with open(refresh_path, mode='w') as f:
f.write(refresh_token + '\n')
client = read_client_secret()
assert 'client_id' in client
print('=== client id ===')
print(client['client_id'])
print()
assert 'client_secret' in client
print('=== client secret ===')
print(client['client_secret'])
print()
address = ('localhost', 46568)
print('=== server url ===')
print('http://{:s}:{:d}/init'.format(*address))
print()
print('Navigate to the page above and follow the instructions.')
print('To kill the server press Ctrl+C on this window.')
print()
server = http.server.HTTPServer(address, UploadToGoogleDriveServer)
server.client = client
server.callback = write_refresh_token
try:
server.serve_forever()
except KeyboardInterrupt:
print()
server.server_close()