from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # Set up the scopes and API credentials scopes = ['https://www.googleapis.com/auth/calendar'] client_id = 'YOUR_CLIENT_ID' client_secret = 'YOUR_CLIENT_SECRET' # Set up the authorization flow flow = InstalledAppFlow.from_client_secrets_file( 'client_secret.json', scopes=scopes) # Run the authorization flow credentials = flow.run_local_server(port=0) # Build the Google Calendar API client service = build('calendar', 'v3', credentials=credentials) # Use the Google Calendar API client to get the user's calendar events events_result = service.events().list(calendarId='primary', maxResults=10, singleEvents=True, orderBy='startTime').execute() events = events_result.get('items', []) if not events: print('No upcoming events found.') else: print('Upcoming events:') for event in events: start = event['start'].get('dateTime', event['start'].get('date')) print(f'{event["summary"]} ({start})')
from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient import errors # Set up the scopes and API credentials scopes = ['https://www.googleapis.com/auth/drive'] client_id = 'YOUR_CLIENT_ID' client_secret = 'YOUR_CLIENT_SECRET' # Set up the authorization flow flow = InstalledAppFlow.from_client_secrets_file( 'client_secret.json', scopes=scopes) # Run the authorization flow credentials = flow.run_local_server(port=0) # Build the Google Drive API client service = build('drive', 'v3', credentials=credentials) # Use the Google Drive API client to list the user's files try: results = service.files().list( pageSize=10, fields="nextPageToken, files(id, name)").execute() items = results.get('files', []) if not items: print('No files found.') else: print('Files:') for item in items: print(f'{item["name"]} ({item["id"]})') except errors.HttpError as error: print(f'An error occurred: {error}')