I’m developing an application that uploads videos to YouTube using the YouTube Data API v3. However, when I upload a video via the API, it ends up being blocked and not available for viewing. I’m encountering several issues and can’t figure out what’s going wrong.
Context:
YouTube API: I’m using YouTube Data API v3.
OAuth 2.0: I’m using OAuth 2.0 for authorization.
Project: The project is in test mode and not published.
Problem.
After uploading a video via the API, it ends up being blocked and inaccessible. This happens even when setting the privacy status to public. The video also does not appear in search results and is only accessible to me.
Steps to Reproduce:
Created a project in Google Developers Console and enabled YouTube Data API v3.
Obtained OAuth 2.0 access token.
Uploaded a video via the API with privacy settings set to public.
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google.oauth2 import service_account
from googleapiclient.http import MediaFileUpload
# Authorization parameters
SCOPES = ['https://www.googleapis.com/auth/youtube.upload']
SERVICE_ACCOUNT_FILE = 'path_to_service_account_key.json'
# Create API client
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
youtube = build('youtube', 'v3', credentials=credentials)
# Upload video
def upload_video(file_path, title, description, category_id):
request_body = {
'snippet': {
'categoryId': category_id,
'title': title,
'description': description
},
'status': {
'privacyStatus': 'public' # Ensure video is set to public
}
}
media = MediaFileUpload(file_path, chunksize=-1, resumable=True)
request = youtube.videos().insert(
part="snippet,status",
body=request_body,
media_body=media
)
response = request.execute()
print(f'Uploaded video ID: {response["id"]}')
# Example function call
upload_video('video.mp4', 'Test Video', 'This is a test video description', '22')
What I Have Tried:
Verified video privacy settings.
Double-checked OAuth 2.0 tokens and permissions.
Reviewed the documentation and sample code at YouTube API.
Question:
Why is a video uploaded via the YouTube API being blocked and inaccessible, despite setting the privacy status to public? How can I resolve this issue and make the video available for public viewing?
Any help or advice would be greatly appreciated.
Roman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2