12 KiB
YouTube Data API Setup Guide
This guide provides step-by-step instructions for setting up YouTube Data API integration for managing video comments.
Table of Contents
- Overview
- Prerequisites
- Google Cloud Console Setup
- Environment Configuration
- OAuth Redirect URI Configuration
- Permissions & Scopes
- Development vs Production
- Troubleshooting
Overview
API Version: YouTube Data API v3
API Service Name: youtube
API Version: v3
Auth Method: OAuth 2.0
Features Supported
- Fetch channel videos
- Read video comments
- Reply to comments
- Automatic token refresh
Prerequisites
- A Google account with a YouTube channel
- Access to Google Cloud Console
- Videos uploaded to your YouTube channel
Google Cloud Console Setup
Step 1: Create a New Project
- Navigate to Google Cloud Console
- Click on the project selector dropdown at the top
- Click "New Project"
- Enter project details:
- Project Name: e.g., "PX360 YouTube Integration"
- Organization: Select your organization (if applicable)
- Click "Create"
- Select your new project
Step 2: Enable YouTube Data API
- Go to "APIs & Services" → "Library"
- Search for "YouTube Data API v3"
- Click on it and click "Enable"
Step 3: Configure OAuth Consent Screen
- Go to "APIs & Services" → "OAuth consent screen"
- Select "External" user type (unless you have a Google Workspace account)
- Click "Create"
- Fill in the required fields:
- App Name: Your application name
- User Support Email: Your support email
- App Logo: Upload your logo
- Application Home Page: Your website URL
- Authorized Domains: Your domain(s)
- Developer Contact Email: Your email
- Click "Save and Continue"
- Add scopes (click "Add or Remove Scopes"):
https://www.googleapis.com/auth/youtube.readonlyhttps://www.googleapis.com/auth/youtube.force-ssl
- Click "Save and Continue"
- Add test users (for development)
- Click "Save and Continue"
Step 4: Create OAuth 2.0 Credentials
- Go to "APIs & Services" → "Credentials"
- Click "Create Credentials" → "OAuth client ID"
- Select "Web application"
- Configure:
- Name: e.g., "PX360 YouTube Client"
- Authorized JavaScript origins:
- Development:
http://127.0.0.1:8000 - Production:
https://yourdomain.com
- Development:
- Authorized redirect URIs:
- Development:
http://127.0.0.1:8000/social/callback/YT/ - Production:
https://yourdomain.com/social/callback/YT/
- Development:
- Click "Create"
- Download the JSON file - This is your credentials file
Step 5: Save Credentials File
- Rename the downloaded JSON file to
yt_client_secrets.json - Place it in your project's
secrets/directory:your_project/ ├── secrets/ │ ├── gmb_client_secrets.json │ └── yt_client_secrets.json └── ...
The JSON file structure:
{
"web": {
"client_id": "xxxxx.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://127.0.0.1:8000/social/callback/YT/"]
}
}
Environment Configuration
Django Settings (settings.py)
# YouTube Data API Configuration
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent.parent
# YouTube API Configuration
YOUTUBE_CLIENT_SECRETS_FILE = BASE_DIR / 'secrets' / 'yt_client_secrets.json'
YOUTUBE_REDIRECT_URI = 'https://yourdomain.com/social/callback/YT/'
Environment Variables (.env)
YOUTUBE_REDIRECT_URI=https://yourdomain.com/social/callback/YT/
OAuth Redirect URI Configuration
The redirect URI must match exactly what's configured in Google Cloud Console.
Development
http://127.0.0.1:8000/social/callback/YT/
http://localhost:8000/social/callback/YT/
Production
https://yourdomain.com/social/callback/YT/
⚠️ Note: Google accepts both HTTP and HTTPS for
localhost/127.0.0.1, but production must use HTTPS.
Permissions & Scopes
The application requests the following OAuth scopes:
| Scope | Description | Required |
|---|---|---|
https://www.googleapis.com/auth/youtube.readonly |
Read channel, videos, comments | ✅ Yes |
https://www.googleapis.com/auth/youtube.force-ssl |
Post replies to comments | ✅ Yes |
Code Reference
# apps/social/utils/youtube.py
YOUTUBE_SCOPES = [
'https://www.googleapis.com/auth/youtube.readonly',
'https://www.googleapis.com/auth/youtube.force-ssl'
]
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'
Development vs Production
Development Setup
| Setting | Value |
|---|---|
YOUTUBE_REDIRECT_URI |
http://127.0.0.1:8000/social/callback/YT/ |
| Protocol | HTTP allowed for localhost |
| App Verification | Not required for testing |
| User Access | Only added test users |
Production Setup
| Setting | Value |
|---|---|
YOUTUBE_REDIRECT_URI |
https://yourdomain.com/social/callback/YT/ |
| Protocol | HTTPS required |
| App Verification | Required for sensitive scopes |
| User Access | Any Google account |
Google App Verification
For production, if your app requests sensitive scopes (like YouTube), you may need verification:
- Submit your app for verification in Google Cloud Console
- Provide a demo video showing the integration
- Wait for Google's review (can take several days)
YouTube API Quotas
YouTube Data API has strict quota limits:
Default Quota
| Resource | Daily Quota |
|---|---|
| Total API Units | 10,000 units/day |
| Search | 100 units/request |
| Videos List | 1 unit/request |
| Comments List | 1 unit/request |
| Comments Insert | 50 units/request |
Quota Calculation Example
Daily sync of 50 videos:
- 50 video list requests = 50 units
- Comments for each video (avg 5 pages) = 250 units
- Total: ~300 units/day (well within limits)
Requesting Higher Quota
If you need more quota:
- Go to Google Cloud Console → "IAM & Admin" → "Quotas"
- Filter by "YouTube Data API"
- Click "Edit Quotas"
- Submit a request explaining your use case
Troubleshooting
Common Error: "Access Denied - Requested client not authorized"
Cause: OAuth consent screen not configured or app not verified.
Solution:
- Ensure OAuth consent screen is properly configured
- Add user as a test user if app is in testing mode
- Submit app for verification if needed for production
Common Error: "Invalid Grant"
Cause: Authorization code expired or already used.
Solution:
- Authorization codes are single-use and expire quickly
- Ensure your code handles the callback immediately
- Check that redirect URI matches exactly
Common Error: "Quota Exceeded"
Cause: Daily API quota exceeded.
Solution:
- Check quota usage in Google Cloud Console
- Optimize API calls to use fewer units
- Request quota increase if needed
- Wait for quota reset (daily at midnight Pacific Time)
Common Error: "Comments Disabled for This Video"
Cause: Video owner has disabled comments.
Solution:
- This is expected behavior for some videos
- Application handles this gracefully
- Skip videos with disabled comments
Common Error: "No Uploads Playlist ID Found"
Cause: Channel has no uploaded videos or credentials incomplete.
Solution:
- Ensure the YouTube channel has uploaded videos
- Check that the uploads playlist ID is stored in credentials
- Re-authenticate if needed
Common Error: "Token Refresh Failed"
Cause: Refresh token expired or revoked.
Solution:
- Google OAuth tokens expire after 6 months of inactivity
- User must re-authenticate
- Ensure
offlineaccess type is requested
Common Error: "Comment Thread Not Found"
Cause: Comment was deleted or video was removed.
Solution:
- Application handles 404 errors gracefully
- Skip deleted comments during sync
API Rate Limits & Best Practices
Rate Limits
| Resource | Limit |
|---|---|
| Requests per second | Varies by endpoint |
| Daily quota units | 10,000 (default) |
Best Practices
- Batch Requests: Minimize API calls by fetching multiple items
- Pagination: Use page tokens for large result sets
- Caching: Cache video/comment data locally
- Delta Sync: Only fetch new comments since last sync
- Error Handling: Gracefully handle quota and rate limit errors
Token Lifecycle
| Token Type | Lifetime | Notes |
|---|---|---|
| Access Token | ~1 hour | Short-lived |
| Refresh Token | 6 months | Expires with inactivity |
| Offline Access | Indefinite | Requires offline access type |
⚠️ Note: If a refresh token expires, the user must re-authenticate.
Verification
After setup, verify the integration:
- Ensure
yt_client_secrets.jsonis in place - Navigate to
/social/in your application - Click "Connect YouTube"
- Authorize with your Google account
- Verify your channel is detected
- Check if videos are fetched
- Test replying to a comment
Testing in Django Shell
from apps.social.services.youtube import YouTubeService
from apps.social.models import SocialAccount
account = SocialAccount.objects.filter(platform='YT').first()
# Test getting videos
videos = YouTubeService.fetch_user_videos(account)
print(f"Found {len(videos)} videos")
# Test getting comments
if videos:
video_id = videos[0]['id']
comments = YouTubeService.fetch_video_comments(account, video_id)
print(f"Found {len(comments)} comments for video {video_id}")
Sharing Credentials with Google Business
If you're already using Google Business API, you can use the same Google Cloud project:
- Both APIs use OAuth 2.0 with similar configuration
- You can create separate OAuth clients or use the same one
- The
secrets/directory can contain multiple credential files:gmb_client_secrets.json(Google Business)yt_client_secrets.json(YouTube)
Single OAuth Client for Both
You can use a single OAuth client for both services:
- Enable both APIs in Google Cloud Console
- Request scopes for both services during OAuth
- Use the same credentials file
- Update settings to point to the same file:
# If using single OAuth client
GOOGLE_CLIENT_SECRETS_FILE = BASE_DIR / 'secrets' / 'google_client_secrets.json'
GMB_CLIENT_SECRETS_FILE = GOOGLE_CLIENT_SECRETS_FILE
YOUTUBE_CLIENT_SECRETS_FILE = GOOGLE_CLIENT_SECRETS_FILE
Support Resources
- YouTube Data API Documentation
- YouTube API Code Samples
- OAuth 2.0 for Web Server Applications
- YouTube API Quota Calculator
- Google Cloud Console Support
Last Updated: February 2026 API Version: YouTube Data API v3