# How to Get Gmail OAuth Token
This guide will walk you through getting your Gmail API credentials and generating an OAuth token.
## Step 1: Get Gmail API Credentials from Google Cloud Console
### 1.1 Create/Select a Project
1. Go to [Google Cloud Console](https://console.cloud.google.com/)
2. Click the project dropdown at the top
3. Click "New Project" or select an existing one
4. Give it a name (e.g., "Gmail MCP Server")
5. Click "Create"
### 1.2 Enable Gmail API
1. In the Google Cloud Console, go to **APIs & Services** > **Library**
2. Search for "Gmail API"
3. Click on "Gmail API"
4. Click **Enable**
### 1.3 Create OAuth 2.0 Credentials
1. Go to **APIs & Services** > **Credentials**
2. Click **+ CREATE CREDENTIALS** > **OAuth client ID**
3. If prompted, configure the OAuth consent screen:
- Choose **External** (unless you have a Google Workspace)
- Fill in the required fields:
- App name: "Gmail MCP Server" (or any name)
- User support email: Your email
- Developer contact: Your email
- Click **Save and Continue**
- On Scopes page, click **Save and Continue**
- On Test users, add your Gmail address, then click **Save and Continue**
- Review and go back to dashboard
4. Back in Credentials:
- Application type: Select **Desktop app**
- Name: "Gmail MCP Desktop Client" (or any name)
- Click **Create**
5. **Download the credentials:**
- Click the download icon (⬇️) next to your new OAuth client
- Save the file as `credentials.json` in a safe location (e.g., `C:\Users\YourName\credentials.json`)
## Step 2: Generate the OAuth Token
You have two options:
### Option A: Use the Python Script (Easiest)
1. **Install required packages:**
```powershell
pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
```
2. **Run the token generator script:**
```powershell
python get-gmail-token.py
```
3. **Follow the prompts:**
- Enter the path to your `credentials.json` file
- Enter where you want to save the token (e.g., `C:\Users\YourName\token.json`)
- A browser window will open asking you to sign in and authorize
- After authorization, the token will be saved automatically
### Option B: Manual Token Generation
If you prefer to generate the token manually, you can use this Python code:
```python
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
import json
import os
# Gmail API scopes
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly',
'https://www.googleapis.com/auth/gmail.send',
'https://www.googleapis.com/auth/gmail.modify']
def get_gmail_token(credentials_path, token_path):
creds = None
# Check if token already exists
if os.path.exists(token_path):
creds = Credentials.from_authorized_user_file(token_path, SCOPES)
# If there are no (valid) credentials available, let the user log in
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
credentials_path, SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(token_path, 'w') as token:
token.write(creds.to_json())
print(f"Token saved to: {token_path}")
return creds
# Usage
if __name__ == "__main__":
credentials_path = input("Enter path to credentials.json: ")
token_path = input("Enter path to save token.json: ")
get_gmail_token(credentials_path, token_path)
```
## Step 3: Set Environment Variables
After you have both files:
- `credentials.json` (from Google Cloud Console)
- `token.json` (generated by the script)
Set the environment variables:
```powershell
[System.Environment]::SetEnvironmentVariable("GMAIL_CREDENTIALS_PATH", "C:\path\to\credentials.json", "User")
[System.Environment]::SetEnvironmentVariable("GMAIL_TOKEN_PATH", "C:\path\to\token.json", "User")
```
Or use the setup script:
```powershell
.\set-gmail-env-vars.ps1
```
## Step 4: Restart Cursor
After setting up the credentials and token, restart Cursor for the Gmail MCP server to work.
## Troubleshooting
### "This app isn't verified" Warning
- This is normal for personal projects
- Click **Advanced** > **Go to [Your App Name] (unsafe)**
- Click **Allow** to proceed
### Token Expiration
- Tokens can expire after some time
- If the token expires, just run the token generator script again
- The script will automatically refresh or regenerate the token
### Permission Denied Errors
- Make sure you've enabled the Gmail API in Google Cloud Console
- Verify your OAuth consent screen is configured
- Check that you've added your email as a test user (for external apps)
### File Not Found Errors
- Use absolute paths (full paths starting with `C:\`)
- Make sure the credentials.json file exists
- Check file permissions
## Security Notes
⚠️ **Important Security Tips:**
- Never commit `credentials.json` or `token.json` to version control
- Keep these files in a secure location
- Don't share these files with others
- If compromised, revoke access in Google Cloud Console and regenerate