0

My code:

import os
import time
import googleapiclient.discovery
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.http import MediaFileUpload
from datetime import datetime, timedelta

# Initialize YouTube API client
CLIENT_ID = '1048782550791-1vhv0jkkrke8tu2mfg8gltrpvuqm0c7b.apps.googleusercontent.com'
CLIENT_SECRET = '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'
SCOPES = ['https://www.googleapis.com/auth/youtube.upload']

# Set up credentials with refresh token
credentials = Credentials.from_authorized_user_info(
    {
        'client_id': CLIENT_ID,
        'client_secret': CLIENT_SECRET,
        'token_uri': 'https://oauth2.googleapis.com/token',
        'refresh_token': '1//0gAGBUd9_MAMwCgYIARAAGBASNwF-L9IryPB7AhlmGSpQkVgCZt341uHTYZEXEm2smgtNNTXNnQsgvdrzP-aisd6p5u6rJKnJ8gw'
    },
    scopes=SCOPES
)

# Create YouTube API client
youtube = googleapiclient.discovery.build('youtube', 'v3', credentials=credentials)

def upload_video(file_path, scheduled_time):

    request = youtube.videos().insert(
        part='snippet,status',
        body={
            'snippet': {
                'title': 'Your Video Title',
                'description': 'Your Video Description',
            },
            'status': {
                'privacyStatus': 'public',
                'publishAt': scheduled_time.strftime('%Y-%m-%dT%H:%M:%S.000Z')
            }
        },
        media_body=MediaFileUpload(file_path)
    )

    response = request.execute()

    return response['id']  # Return the uploaded video ID

# Define the list of video files
video_files = ['vid1.mov', 'vid2.mov', 'vid3.mov']

# Calculate the scheduled time for the first video
scheduled_time = datetime.utcnow()

# Iterate over your video files and upload each video with a scheduled time
for index, video_file in enumerate(video_files):

    video_path = os.path.join('C:\\Users\\knses\\Desktop\\New folder\\video', video_file)
    upload_video(video_path, scheduled_time)

    # Calculate the scheduled time for the next video
    scheduled_time += timedelta(minutes=15)
    time.sleep(900)  # Sleep for 900 seconds (15 minutes) between each upload

This is my error:

Exception has occurred: HttpError

<HttpError 403 when requesting https://youtube.googleapis.com/upload/youtube/v3/videos?part=snippet%2Cstatus&alt=json&uploadType=multipart returned "The request cannot be completed because you have exceeded your <a href="/youtube/v3/getting-started#quota">quota</a>.". Details: "[{'message': 'The request cannot be completed because you have exceeded your <a href="/youtube/v3/getting-started#quota">quota</a>.', 'domain': 'youtube.quota', 'reason': 'quotaExceeded'}]">

  File "C:\Users\knses\Desktop\New folder\import sos.py", line 43, in upload_video

    response = request.execute()

               ^^^^^^^^^^^^^^^^^

  File "C:\Users\knses\Desktop\New folder\import sos.py", line 56, in <module>

    upload_video(video_path, scheduled_time)

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://youtube.googleapis.com/upload/youtube/v3/videos?part=snippet%2Cstatus&alt=json&uploadType=multipart returned "The request cannot be completed because you have exceeded your <a href="/youtube/v3/getting-started#quota">quota</a>.". Details: "[{'message': 'The request cannot be completed because you have exceeded your <a href="/youtube/v3/getting-started#quota">quota</a>.', 'domain': 'youtube.quota', 'reason': 'quotaExceeded'}]">

I was trying to make a code using YouTube api v3 which allows to upload video in 15 minutes interval schedule of every video i wanted to do

mousetail
  • 7,009
  • 4
  • 25
  • 45

0 Answers0