-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_token.py
More file actions
34 lines (28 loc) · 1.12 KB
/
Copy pathget_token.py
File metadata and controls
34 lines (28 loc) · 1.12 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
import pickle
import os
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/drive.file']
def main():
creds = None
# Если token.json уже есть — используем его
if os.path.exists('token.json'):
with open('token.json', 'rb') as token:
creds = pickle.load(token)
# Если нет или не валиден — запрашиваем новый
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
# Используем скачанный client_secret файл
flow = InstalledAppFlow.from_client_secrets_file(
'client_secret.json',
SCOPES
)
creds = flow.run_local_server(port=0)
# Сохраняем token.json
with open('token.json', 'wb') as token:
pickle.dump(creds, token)
print("✅ token.json создан!")
if __name__ == '__main__':
main()