Google Calendar
This documentation provides step-by-step instructions for setting up the Google Calendar API and configuring OAuth2 tokens, with detailed focus on the authorization flow steps 4 and 5.
Prerequisites
Google account
Postman (for API testing)
Basic understanding of OAuth2 flow
Web application or development environment
Step 1: Create a Google Cloud Project
Navigate to the Google Cloud Console
Click "Select a project" → "New Project"
Enter a project name (e.g., "My Calendar Integration")
Click "Create"
Step 2: Enable Google Calendar API
In the Google Cloud Console, go to "APIs & Services" → "Library"
Search for "Google Calendar API"
Click on "Google Calendar API" and click "Enable"
Step 3: Configure OAuth2 Consent Screen
Go to "APIs & Services" → "OAuth consent screen"
Choose "External" (for testing) or "Internal" (for organization use)
Fill in the required information:
App name
User support email
Developer contact information
Add scopes:
https://www.googleapis.com/auth/calendar
Add test users (if using External type)
Click "Save and Continue" through all steps
Step 4: Create OAuth2 Credentials
Go to "APIs & Services" → "Credentials"
Click "Create Credentials" → "OAuth 2.0 Client IDs"
Choose "Web application" as the application type
Configure the OAuth client:
Name: Give your OAuth client a descriptive name
Authorized JavaScript origins: Add your domain (e.g.,
http://localhost:3000
for development)Authorized redirect URIs: Add your callback URL (e.g.,
http://localhost:3000/callback
)
Click "Create"
Important: Copy and securely store the generated:
Client ID
Client Secret
OAuth2 Client Configuration Details
{
"web": {
"client_id": "your-client-id.apps.googleusercontent.com",
"project_id": "your-project-id",
"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_secret": "your-client-secret",
"redirect_uris": ["http://localhost:8080"]
}
}
Step 5: Implement OAuth2 Authorization Flow
5.1 Generate Authorization URL
Create the authorization URL to redirect users to Google's OAuth2 server:
https://accounts.google.com/o/oauth2/v2/auth?
client_id=YOUR_CLIENT_ID&
response_type=code&
scope=https://www.googleapis.com/auth/calendar&
access_type=offline&
redirect_uri=http://localhost:8081&
state=RANDOM_STATE_STRING
Parameters Explanation:
client_id
: Your OAuth2 client ID from Step 4response_type=code
: Indicates authorization code flowscope
: Calendar access permissionaccess_type=offline
: Enables refresh token generationredirect_uri
: Must match the URI configured in Step 4state
: Security parameter to prevent CSRF attacks
This can be done with the following curl code
5.2 Handle Authorization Response
After user consent, Google redirects to your callback URL with:
Success:
?code=AUTHORIZATION_CODE&state=STATE_VALUE
Error:
?error=access_denied&state=STATE_VALUE
5.3 Exchange Authorization Code for Tokens
Using Postman for Token Exchange:
Create a new POST request in Postman
URL:
https://oauth2.googleapis.com/token
Headers:
Content-Type: application/x-www-form-urlencoded
Body (select "x-www-form-urlencoded"):
client_id: YOUR_CLIENT_ID client_secret: YOUR_CLIENT_SECRET code: AUTHORIZATION_CODE_FROM_CALLBACK grant_type: authorization_code redirect_uri: YOUR_REDIRECT_URI
curl --location 'https://oauth2.googleapis.com/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'code=<code retrieved above>' \
--data-urlencode 'client_id=<clientId>' \
--data-urlencode 'client_secret=<client Secret>' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'redirect_uri=https://localhost:8081'
Expected Response:
{
"access_token": "ya29.a0AfH6SMC...",
"expires_in": 3599,
"refresh_token": "1//04-xxxxxxxxxxx",
"scope": "https://www.googleapis.com/auth/calendar",
"token_type": "Bearer"
}
5.4 Store Tokens Securely
Access Token: Short-lived (1 hour), used for API requests
Refresh Token: Long-lived, used to generate new access tokens
Store both tokens securely (encrypted database, secure environment variables)
Additional Resources
Last updated