- 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 Invokerfor 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
.envfile (or create one). -
Add the following variables:
GOOGLE_CALENDAR_CREDENTIALS='PASTE_YOUR_JSON_HERE' GOOGLE_CALENDAR_ID='your_calendar_id@gmail.com'
⚠️ Important — format details:- The JSON must be a single line (no literal newlines).
- The private key section must have its newlines escaped as
\\n. - You can use a tool like
jqor Python to minify your service account JSON.
Example
.enventry:GOOGLE_CALENDAR_CREDENTIALS='{"type":"service_account","project_id":"your-project-id","private_key_id":"abc123","private_key":"-----BEGIN PRIVATE KEY-----\\nMIIEv...\\n-----END PRIVATE KEY-----\\n","client_email":"your-service-account@project.iam.gserviceaccount.com","client_id":"1234567890","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/your-service-account.iam.gserviceaccount.com"}'Quick conversion commands:
Using jq (Linux/macOS):
jq -c . service_account.jsonUsing Python (cross-platform):
python -c "import json;print(json.dumps(json.load(open('service_account.json'))))"- To find your
GOOGLE_CALENDAR_ID:
Go to Google Calendar → Settings → Integrate Calendar → copy the Calendar ID (it looks like an email).
Run the following command to install the required library:
pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-clientNow, 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)