pl-jay
pl-jay

Reputation: 1100

How to automate Youtube upload using python

When im executing this code,


    import datetime
    from googleapiclient.http import MediaFileUpload
    from googleapiclient.discovery import build
    from google_auth_oauthlib.flow import InstalledAppFlow
    from apikey import apikey
    
    CLIENT_SECRET_FILE = 'client_secret.json'
    SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl']
    flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
    credentials = flow.run_console()
    youtube = build('youtube', 'v3', credentials=credentials)
    
    upload_date_time = datetime.datetime(
        2020, 8, 25, 12, 30, 0).isoformat() + '.000Z'
    
    request_body = {
        'snippet': {
            'categoryI': 19,
            'title': 'Upload Testing This is Private Video ',
            'description': 'Upload TEsting This is Private Video',
            'tags': ['Python', 'Youtube API', 'Google']
        },
        'status': {
            'privacyStatus': 'private',
            'publishAt': upload_date_time,
            'selfDeclaredMadeForKids': False,
        },
        'notifySubscribers': False
    }
    
    mediaFile = MediaFileUpload('3.mp4')
    
    response_upload = youtube.videos().insert(
        part='snippet,status',
        body=request_body,
        media_body=mediaFile
    ).execute()
    
    
    youtube.thumbnails().set(
        videoId=response_upload.get('id'),
        media_body=MediaFileUpload('thumbnail.png')
    ).execute()

it asks for this,

Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=65em7.apps.googleusercontent.com&redirect_uri=urn%tf%3g%h%32.%3b&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.force-ssl&state=hy5Q2tM5&prompt=consent&access_type=offline

And once accessed this link i have to manually login to google account and get the Auth code and provide it in the console. right here

Enter the authorization code:

How do i automate this process using python ?

Thanks in advance.

Upvotes: 1

Views: 413

Answers (1)

Galunid
Galunid

Reputation: 578

You can open the link using selenium, make it click authenticate, login with your credentials and then get the code. It will work in your case, but you still need to get user's login and password. This should be avoided and a smart user wouldn't do it, hence why this mechanism was implemented in the first place. User uses a trusted site to confirm they want to allow your app to do <your app permissions go here>.

Upvotes: 2

Related Questions