Google Gmail API & Calendar API Setup Guide

From creating a Google Cloud project, enabling the APIs, configuring the OAuth consent screen, all the way to getting credentials and authorizing a test user — every step in this guide was verified hands-on in the Google Cloud Console in July 2026, with screenshots redrawn from the actual UI.

Tested: 2026-07 UI language: Traditional Chinese Level: Beginner Cost: Free

Contents

  1. Prerequisites
  2. Create a Google Cloud project
  3. Enable the Gmail API
  4. Enable the Google Calendar API
  5. Configure the OAuth consent screen (Google Auth Platform)
  6. Create an OAuth client ID (credentials)
  7. Configure data access (scopes)
  8. Add a test user
  9. Call the API (Python example)
  10. FAQ
0

Prerequisites

All you need is a Google account (a regular Gmail account works fine). No credit card is required anywhere in this flow: both the Gmail API and Calendar API have generous free quotas, more than enough for individual developers.

Create project
Enable 2 APIs
OAuth consent screen
Create credentials
Scopes + test user
Write code & call it
Why all these steps?Gmail and Calendar data are "the user's private data" — Google requires apps to get the user's consent via OAuth 2.0 before accessing them. So beyond turning on the APIs, you also need to set up a "consent screen" and "authorized scopes".
1

Create a Google Cloud project

Open https://console.cloud.google.com/projectcreate (or click "New Project" from the project dropdown at the top of the console).

console.cloud.google.com/projectcreate
New Project
api-course-demo
Project ID: api-course-demo. The ID cannot be changed once set. Edit
No organization Browse
Select an organization or folder to create this resource under
CreateCancel
▲ Redrawn from the actual UI: the "New Project" page. Enter a project name and click "Create".
  1. In "Project name" enter something memorable, e.g. api-course-demo (a project ID is auto-generated below the name and cannot be changed later).
  2. For a personal account, leave "Location" as "No organization".
  3. Click Create. After about 10 seconds the bell icon in the top right shows a notification "Creating project: api-course-demo ✔".
  4. Click "Select project" in the notification, or switch to the new project via the project dropdown at the top. From here on, always confirm the top-left corner shows this project.
NoteFree accounts default to a quota of about 10–12 projects. If clicking "Create" does nothing, delete an old project or request a quota increase.
2

Enable the Gmail API

Top-left menu ☰ → APIs & ServicesLibrary, search "Gmail"; or open directly https://console.cloud.google.com/apis/library/gmail.googleapis.com.

console.cloud.google.com/apis/library/gmail.googleapis.com?project=api-course-demo
🔽 api-course-demo
✉️
Gmail API
Google Enterprise API · View and manage Gmail mailbox data.
EnableTry this API
▲ Redrawn from the actual UI: the Gmail API product page, click the blue "Enable" button.

After clicking Enable, it spins for about 5–10 seconds, then automatically lands on the "API/Service Details" page, showing Enabled. A banner at the top will suggest "You may need credentials to call this API from your own application" — we'll create credentials in step 5.

3

Enable the Google Calendar API

Same process: search the library for "Calendar" and choose Google Calendar API; or open directly https://console.cloud.google.com/apis/library/calendar-json.googleapis.com.

console.cloud.google.com/apis/library/calendar-json.googleapis.com?project=api-course-demo
🔽 api-course-demo
📅
Google Calendar API
Google Enterprise API · Manage calendars and events in Google Calendar.
EnableTry this API
▲ Redrawn from the actual UI: the Google Calendar API product page (service name calendar-json.googleapis.com).

Click Enable; once the status changes to "Enabled" you're done. The project now has both APIs available — next we handle authorization.

4

Configure the OAuth consent screen (Google Auth Platform)

Left sidebar APIs & ServicesOAuth consent screen, which takes you to the "Google Auth Platform". A new project will show "Google Auth Platform not configured" — click Get Started.

console.cloud.google.com/auth/overview?project=api-course-demo
🖼️
Google Auth Platform not configured
Get started configuring your application's identity, and manage the credentials used to call Google APIs and "Sign in with Google".
Get Started
▲ Redrawn from the actual UI: the initial OAuth overview page.

The setup wizard has 4 steps (as tested):

  1. App Information: for "App name" enter something like API Course Demo (this is what users see on the consent screen); for "User support email" pick your own Gmail from the dropdown. Click "Next".
  2. Audience: choose External. A personal Gmail account has no Workspace organization, so External is the only option; the app will launch in "Testing" mode, usable only by accounts on the test user list. Click "Next".
  3. Contact Information: enter your email (used for Google project change notifications). Click "Next".
  4. Finish: check "I agree to the Google API Services: User Data Policy" → click "Continue" → click Create. A "OAuth configuration created!" message appears at the bottom.
console.cloud.google.com/auth/overview/create?project=api-course-demo
✔ App Information  ✔ Audience  ✔ Contact Information  ❹ Finish
I agree to the Google API Services: User Data Policy.
Continue
CreateCancel
▲ Redrawn from the actual UI: the final step of the 4-step wizard.
5

Create an OAuth client ID (credentials)

Google Auth Platform left sidebar → ClientsCreate Client (or APIs & Services → Credentials → Create Credentials → OAuth client ID).

console.cloud.google.com/auth/clients/create?project=api-course-demo
Create OAuth Client ID
Desktop app ▾
Options: Web application / Android / Chrome extension / iOS / TV and limited-input devices / Desktop app
Desktop client 1
Note: it may take 5 minutes to a few hours for settings to take effect
CreateCancel
▲ Redrawn from the actual UI: for local scripts/course exercises, "Desktop app" is simplest.

After clicking Create, an "OAuth client created" dialog pops up:

console.cloud.google.com/auth/clients — dialog
OAuth client created
You can find this client ID anytime in the "Clients" tab of Google Auth Platform.
ⓘ Only test users listed on the OAuth consent screen have OAuth access
Client ID1454707•••••-9t3e7t•••••••••••••.apps.googleusercontent.com 📋
⬇ Download JSON
OK
▲ Redrawn from the actual UI: be sure to click "Download JSON" and save the file as credentials.json.
Important: keep the JSON credentials file safeThe downloaded client_secret_xxx.json file contains your client_id and client_secret — it's effectively the key to your app. Rename it to credentials.json, place it in your project folder, add it to .gitignore, and never upload it to a public GitHub repo.
6

Configure data access (scopes)

Google Auth Platform → Data AccessAdd or Remove Scopes. A "Update selected scopes" panel slides out from the right, listing all scopes for your enabled APIs (which is why we did steps 2 and 3 first).

  1. In "Filter" type gmail.send and press Enter, then check the Gmail API scope .../auth/gmail.send ("Send email on your behalf").
  2. Clear the filter, type calendar.events, and check the Google Calendar API scope .../auth/calendar.events ("View and edit events on all your calendars").
  3. Scroll to the bottom of the panel and click Update, then back on the page click Save.
console.cloud.google.com/auth/scopes?project=api-course-demo
🔒 Your sensitive scopes
APIScopeUser-facing description
Gmail API.../auth/gmail.sendSend email on your behalf 🗑
Google Calendar API.../auth/calendar.eventsView and edit events on all your calendars 🗑
SaveDiscard changes
▲ Redrawn from the actual UI: both scopes fall under "sensitive scopes" and can be used directly while in testing mode.
Scope selection: keep it minimalCommon options — read-only mail: gmail.readonly; send-only: gmail.send; full mailbox: https://mail.google.com/ (a restricted scope requiring security review for production); calendar read/write: calendar.events; calendar read-only: calendar.events.readonly. The narrower the scope, the simpler the review and the lower the risk.
7

Add a test user

While your app is in "Testing" status, only accounts on the test user list can complete OAuth authorization. Google Auth Platform → Audience → in the test users section click + Add users.

console.cloud.google.com/auth/audience?project=api-course-demo
Test users
1 user (1 test user, 0 other users) / limit 100
+ Add users
User info
your-account@gmail.com 🗑
▲ Redrawn from the actual UI: enter your Gmail and click "Save"; success is confirmed when the account appears in the list.

Enter the Gmail address you'll use for testing (usually your own account) and click Save. The limit is 100 users.

Want to launch this to everyone?At the top of the "Audience" page there's a "Publish App" button to switch to production. Apps using sensitive/restricted scopes require Google verification (days to weeks). For personal use or course exercises, staying in testing mode is fine — the only downside is the refresh token expires after 7 days, after which you just re-authorize once.
8

Call the API (Python example)

Put the credentials.json file downloaded in step 5 in the same folder as your script, then install the packages:

pip install google-auth-oauthlib google-api-python-client

The first run automatically opens a browser; sign in with your test user account and grant consent (you'll see "Google hasn't verified this app" — just click "Continue", since it's your own app). After authorizing, the token is cached locally so you won't need to sign in again.

# demo.py — send an email + create a calendar event
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import base64, os.path, pickle
from email.mime.text import MIMEText

SCOPES = [
    "https://www.googleapis.com/auth/gmail.send",
    "https://www.googleapis.com/auth/calendar.events",
]

# --- OAuth authorization (opens a browser the first time) ---
creds = None
if os.path.exists("token.pickle"):
    with open("token.pickle", "rb") as f:
        creds = pickle.load(f)
if not creds or not creds.valid:
    flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
    creds = flow.run_local_server(port=0)
    with open("token.pickle", "wb") as f:
        pickle.dump(creds, f)

# --- Gmail: send an email ---
gmail = build("gmail", "v1", credentials=creds)
msg = MIMEText("Hello, this email was sent via the Gmail API!")
msg["to"] = "someone@example.com"
msg["subject"] = "Gmail API test"
raw = base64.urlsafe_b64encode(msg.as_bytes()).decode()
gmail.users().messages().send(userId="me", body={"raw": raw}).execute()
print("Email sent!")

# --- Calendar: create an event ---
cal = build("calendar", "v3", credentials=creds)
event = {
    "summary": "API course exercise",
    "start": {"dateTime": "2026-07-20T10:00:00+08:00"},
    "end":   {"dateTime": "2026-07-20T11:00:00+08:00"},
}
created = cal.events().insert(calendarId="primary", body=event).execute()
print("Event created:", created.get("htmlLink"))
9

FAQ

Q1: "Access denied" error during authorization (403: access_denied)?

The signed-in account isn't on the test user list. Go back to step 7 and add it.

Q2: "Google hasn't verified this app" warning appears?

Normal for testing mode. Click "Continue" (sometimes hidden under an "Advanced" link) — it's fine since this is your own app.

Q3: The token stops working after a few days and asks you to sign in again?

In testing mode, the refresh token is valid for 7 days. Delete token.pickle and re-authorize, or publish the app to production.

Q4: Can't find the "OAuth consent screen" menu item?

Since 2024, Google folded it into "Google Auth Platform". Path: APIs & Services → OAuth consent screen, or open console.cloud.google.com/auth/overview directly.

Q5: The API is enabled, but the program still reports "API not enabled"?

You likely selected the wrong project. Check the project name in the top-left of the console, and confirm credentials.json was downloaded from that same project.