Google Calendar OAuth 2.0 Setup
Setup OAuth 2.0 Client
-
Create a Google OAuth 2.0 client ID by following the steps in the Google API Console OAuth 2.0 setup guide. Once created, copy your
CLIENT_ID
andCLIENT_SECRET
values. - Ensure Google Calendar API is enabled for the project.
-
Configure permission scopes to at least include:
calendar.events.readonly
userinfo.email
Configure these in the OAuth Consent Screen section.
-
Add Authorized redirect URI in the Credentials section:
https://your-domain.com/auth/google/callback
Implement OAuth 2.0 Authorization Code Flow
Step 1: Generate Authorization URL
Create an endpoint that generates the Google OAuth authorization URL: Endpoint:POST /api/auth/google/url
Request Body:
Step 2: Handle OAuth Callback
When users authorize your application, Google redirects them to your callback URL with either an authorization code or an error. Endpoint:GET /auth/google/callback
Query Parameters:
code
- Authorization code (on success)error
- Error description (on failure)
Step 3: Exchange Authorization Code for Tokens
Endpoint:POST /api/auth/google/exchange-token
Request Body:
Step 4: Using the Refresh Token
Store theoauth_refresh_token
securely. This long-lived token allows you to:
Generate Access Tokens:
Important Configuration Notes
-
Redirect URI Consistency: Use the same redirect URI across all steps:
- OAuth client configuration in Google Console
- Authorization URL generation
- Token exchange endpoint
-
Refresh Token Requirements:
access_type: 'offline'
- Required for refresh tokenprompt: 'consent'
- Forces consent screen to ensure refresh token- First-time authorization only - subsequent authorizations may not return refresh token
-
Security Considerations:
- Store client credentials securely (environment variables)
- Validate all input parameters
- Use HTTPS for all endpoints
- Store refresh tokens securely (encrypted database)