- Go to the Google Cloud Console.
- Click on the project dropdown (top-left) → New Project.
- Enter a Project Name (e.g., "WhatsApp Bot Calendar").
- Click Create.
- Inside your new project, go to the Google API Library.
- Search for Google Calendar API.
- Click Enable.
- Open Service Accounts.
- Click Create Service Account.
- Service Account Name: Give it a name (e.g., "Calendar Bot").
- Click Create and Continue.
- Grant Role: Select Editor or Owner (or
Cloud Functions Invoker
for limited access). - Click Done.
- In the Service Accounts section, find your newly created service account.
- Click on it → Keys tab → Add Key → Create New Key.
- Choose JSON, then click Create.
- A JSON file will download. Keep it safe—this is your credential file!
- Go to Google Calendar.
- Click on Settings (⚙️) → Settings.
- Scroll to "My Calendars" and select the calendar you want to use.
- Under "Share with specific people", click Add people.
- Enter the service account email (found in your JSON file).
- Set permissions to "Make changes to events".
- Click Send.
- Open your
.env
file (or create one). - Add the following:
GOOGLE_CALENDAR_CREDENTIALS='PASTE_YOUR_JSON_HERE' GOOGLE_CALENDAR_ID='your_calendar_id@gmail.com'
- To find your
GOOGLE_CALENDAR_ID
:- Go to Google Calendar settings.
- Under "Integrate Calendar", copy the "Calendar ID" (it looks like an email).
- To find your
Run the following command to install the required library:
pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
Now, update your Python code to use the credentials:
from google.oauth2 import service_account
from googleapiclient.discovery import build
import os
import json
# Load credentials
creds_json = json.loads(os.getenv("GOOGLE_CALENDAR_CREDENTIALS"))
credentials = service_account.Credentials.from_service_account_info(creds_json, scopes=["https://www.googleapis.com/auth/calendar"])
# Build the Google Calendar service
service = build("calendar", "v3", credentials=credentials)