Quiero usar la API de Google Calendar para mi aplicación Django. He seguido las instrucciones aquí: https://karenapp.io/articles/how-to-automate-google-calendar-with-python-using-the-calendar-api/
También agregué los uri de redirección en la API de Google: parece que un navegador intenta abrirse en el lado del servidor (como lo hace en local, pero no puedo manipularlo porque el navegador del lado del servidor no aparece). En la terminal veo "Visite esta URL para autorizar esta aplicación: https://accounts.google.com/..."
¿Alguna idea sobre lo que puedo hacer?
Código en vistas.py:
import datetime import pickle import os.path from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request SCOPES = ['https://www.googleapis.com/auth/calendar'] CREDENTIALS_FILE = 'path_to_file/credentials.json' def get_calendar_service(): creds = None if os.path.exists('token.pickle'): with open('token.pickle', '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: flow = InstalledAppFlow.from_client_secrets_file( CREDENTIALS_FILE, SCOPES) creds = flow.run_local_server(port=0) with open('token.pickle', 'wb') as token: pickle.dump(creds, token) service = build('calendar', 'v3', credentials=creds) return serviceEl problema que tiene es que está utilizando el flujo de la aplicación instalada.
flow = InstalledAppFlow.from_client_secrets_file( CREDENTIALS_FILE, SCOPES) creds = flow.run_local_server(port=0)Este código fue diseñado para usarse con aplicaciones instaladas y, por lo tanto, abrirá la ventana del navegador web en la máquina en la que se está ejecutando actualmente.
Uso de OAuth 2.0 para aplicaciones de servidor web
import google.oauth2.credentials import google_auth_oauthlib.flow # Use the client_secret.json file to identify the application requesting # authorization. The client ID (from that file) and access scopes are required. flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file( 'client_secret.json', scopes=['https://www.googleapis.com/auth/drive.metadata.readonly']) # Indicate where the API server will redirect the user after the user completes # the authorization flow. The redirect URI is required. The value must exactly # match one of the authorized redirect URIs for the OAuth 2.0 client, which you # configured in the API Console. If this value doesn't match an authorized URI, # you will get a 'redirect_uri_mismatch' error. flow.redirect_uri = 'https://www.example.com/oauth2callback' # Generate URL for request to Google's OAuth 2.0 server. # Use kwargs to set optional request parameters. authorization_url, state = flow.authorization_url( # Enable offline access so that you can refresh an access token without # re-prompting the user for permission. Recommended for web server apps. access_type='offline', # Enable incremental authorization. Recommended as a best practice. include_granted_scopes='true')