I'm testing a python script using the YouTube Data API to update my YouTube thumbnails automatically for A/B testing.
Each iteration sets a new thumbnail and retrieves video stats using the methods in the following class:
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google_auth_oauthlib import flow
class YouTubeClient:
def __init__(self, client_secrets_file):
self.api_service_name = "youtube"
self.api_version = "v3"
self.scopes = [".force-ssl"]
self.flow = flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes=self.scopes
)
self.credentials = self.flow.run_local_server()
self.youtube_client = build(
self.api_service_name, self.api_version, credentials=self.credentials
)
def set_thumbnail(self, video_id, thumbnail_path):
request = self.youtube_client.thumbnails().set(
media_body=MediaFileUpload(thumbnail_path),
videoId=video_id
)
request.execute()
print(f"Updated thumbnail to {thumbnail_path}")
def get_video_stats(self, video_id):
request = self.youtube_client.videos().list(
part="statistics",
id=video_id
)
response = request.execute()
stats = response["items"][0]["statistics"]
print(f"Retrieved stats: {stats}")
return int(stats["viewCount"])
The iteration must have run maybe 10-15 times within an hour yesterday evening. I hit the rate limit and got the following error:
googleapiclient.errors.HttpError: <HttpError 429 when requesting ;alt=json&uploadType=media returned "The user has uploaded too many thumbnails recently. Please try the request again later.". Details: "[{'message': 'The user has uploaded too many thumbnails recently. Please try the request again later.', 'domain': 'youtube.thumbnail', 'reason': 'uploadRateLimitExceeded'}]">
After looking it up, I saw that there are 10,000 free "units" per day for the API. And yet, setting the thumbnail counts for 50 units, so that would allow for 200 thumbnail updates - and I definitely haven't done that many!
I understand that the limit may be combined with the stats retrieval but I still don't get how I could have hit the limit. Also, the error message is specific to thumbnails, which makes me think there is a "hidden" rate limit for thumbnails. I thought it might be because I updated the thumbnail a lot in a short space of time (about 1 hour) but it is still not working the morning after...
When looking in the Google Developer Console, I see this, which just makes me even more confused :
This seems to confirm that the quotas are not surpassed.
What is going wrong? How long does the rate limit take to reset?
Any help would be appreciated, thank you!