0

I noticed when YouTube allowed for custom playlist thumbnails that the API documentation updated to reflect it. However every time I try, I get errors.

Here is the code I tried

import os
import json
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload

CLIENT_SECRETS_FILE = "client_secrets.json"
SCOPES = "https://www.googleapis.com/auth/youtube.force-ssl"
REDIRECT_PORT = 9000
PLAYLIST_ID = "PLlYdC6fGRAbE0nwjMzm8-v6_52IXr4t9S"
THUMBNAIL_PATH = "Darlin2.jpg"

def get_authenticated_service():
    flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
    credentials = flow.run_local_server(port=REDIRECT_PORT)
    return build("youtube", "v3", credentials=credentials)

def upload_playlist_thumbnail(youtube, playlist_id, thumbnail_path):
    print("Uploading playlist thumbnail...")

    media = MediaFileUpload(thumbnail_path, mimetype='image/jpeg')

    request = youtube.playlistImages().insert(
        part="snippet",
        body={
            "snippet": {
                "playlistId": playlist_id,
                "type": "banner"
            }
        },
        media_body=media
    )

    response = request.execute()
    print("Thumbnail uploaded successfully.")
    print(json.dumps(response, indent=2))

if __name__ == "__main__":
    youtube = get_authenticated_service()
    upload_playlist_thumbnail(youtube, PLAYLIST_ID, THUMBNAIL_PATH)

And this is the error I get...

Uploading playlist thumbnail...
Traceback (most recent call last):
  File "c:\Users\lysa.moulton\OneDrive - Concord\Documents\YouTube API\standalone_scripts\playlistthumbupdate.py", line 44, in <module>
    upload_playlist_thumbnail(youtube, PLAYLIST_ID, THUMBNAIL_PATH)
  File "c:\Users\lysa.moulton\OneDrive - Concord\Documents\YouTube API\standalone_scripts\playlistthumbupdate.py", line 27, in upload_playlist_thumbnail
    request = youtube.playlistImages().update(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\lysa.moulton\AppData\Local\Programs\Python\Python311\Lib\site-packages\googleapiclient\discovery.py", line 1048, in method
    raise TypeError("Got an unexpected keyword argument {}".format(name))
TypeError: Got an unexpected keyword argument media_body
2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Jun 24 at 23:05
  • I want to change/add an image to my youtube playlist via youtube's api. Commented Jul 7 at 14:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.