-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_token.py
More file actions
32 lines (26 loc) · 1.17 KB
/
Copy pathget_token.py
File metadata and controls
32 lines (26 loc) · 1.17 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
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
# Permiso de solo lectura para el calendario
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
def main():
creds = None
# Verifica si ya existe el token
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# Si no hay credenciales válidas, pide al usuario que se loguee
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Guarda las credenciales para la próxima vez
with open('token.json', 'w') as token:
token.write(creds.to_json())
print("✅ ¡Éxito! El archivo token.json se ha generado correctamente.")
else:
print("✅ El archivo token.json ya existía y es válido.")
if __name__ == '__main__':
main()