Steps to connect to Google calender
Steps to connect to Google calender
Step one
- Go to the Google Developers Console.
- Create a new project or select an existing one.
- Search for the Google Calendar API and enable it.
- Go to the “Credentials” tab and create credentials for a Desktop application.(This will download a credentials.json file.)
Step 2: Install Google Client Library
- Open your terminal or command prompt and install the Google Client Library by running: pip install –upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
# Step 3:
- Python code from research:
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
import datetime
def authenticate_google_calendar():
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
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)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
return creds
def add_event_to_calendar(summary, location, description, start_time, end_time, timezone='America/Los_Angeles'):
creds = authenticate_google_calendar()
service = build('calendar', 'v3', credentials=creds)
event = {
'summary': summary,
'location': location,
'description': description,
'start': {
'dateTime': start_time,
'timeZone': timezone,
},
'end': {
'dateTime': end_time,
'timeZone': timezone,
},
}
event = service.events().insert(calendarId='primary', body=event).execute()
print(f"Event created: {event.get('htmlLink')}")
# Example usage
add_event_to_calendar(
summary='Example Event',
location='800 Howard St., San Francisco, CA 94103',
description='A chance to hear more about Google\'s developer products.',
start_time='2024-01-30T09:00:00',
end_time