I'm new to Google Cloud Platform and I'm trying to build a Python script that automatically uploads videos to YouTube. My goal is to run this script on a server, so I'm using a service account to avoid manual login prompts. However, I'm facing an "Unauthorized" error with the message "youtubeSignupRequired" during the upload process.
Here's a breakdown of the steps I've taken:
- Created a Google Cloud project and enabled the YouTube Data API v3.
- Created a service account. I was unable to find a "YouTube Content Manager" role, so I assigned the "Video Stitcher Admin" role, assuming it would grant the necessary permissions.
- Downloaded the service account's JSON credentials file.
- Installed the required Python libraries:
google-api-python-client,google-auth-httplib2,google-auth-oauthlib.
My Python code for authentication and uploading is as follows:
import osimport google.authimport google_auth_oauthlib.flowimport googleapiclient.discoveryimport googleapiclient.errorsimport googleapiclient.httpimport jsonfrom jinja2 import Template# Scopes required to upload a videoSCOPES = ["https://www.googleapis.com/auth/youtube.upload"]def authenticate_youtube(): credentials = None SERVICE_ACCOUNT_FILE = 'path/to/your/key.json' # Replaced with the path to my JSON file if os.path.exists(SERVICE_ACCOUNT_FILE): credentials = google.oauth2.service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_FILE, scopes=SCOPES) if not credentials: print('Error: Could not find service account credentials.') exit(1) youtube = googleapiclient.discovery.build('youtube', 'v3', credentials=credentials) return youtube# ... (rest of upload_video code) Despite these steps, I consistently encounter the following error when running the script:
googleapiclient.errors.ResumableUploadError: <HttpError 401 when requesting None returned "Unauthorized". Details: "[{'message': 'Unauthorized', 'domain': 'youtube.header', 'reason': 'youtubeSignupRequired', 'location': 'Authorization', 'locationType': 'header'}]">I've verified the following:
- The YouTube Data API v3 is enabled in my project.
- The scopes in my code are correct for video uploads.
- The JSON credentials file is valid and in the correct location.
- My API usage quotas are not exceeded.
I'm unsure what else to check. Could someone please guide me? Is the "Video Stitcher Admin" role insufficient for this task? Have I missed a crucial configuration step in Google Cloud specific to using service accounts for YouTube uploads? Any assistance would be greatly appreciated!