I've been using the Youtube API to add multiple playlistItems
to playlists
on my profile, but doing so with loops tends to use up my daily request quota and I've hundreds to add.
- I'm sending <50 video IDs per batch
- the video IDs are all valid, and can be added succesfully one at a time
The new_batch_http_request
method seems to be the solution but roughly 1 in every 4 video ids within the batch ends up failing. On Googling the problem it might be related to the position
leading to overwrites?
Using this example, the first and third items were successful but the 2nd failed:
from Google import Create_Service
CLIENT_SECRET_FILE = 'client_secret_file.json'
API_NAME = 'youtube'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtube.force-ssl']
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
playlistItemsList = ['eDTnKLqFuYU', 'TT7plfxWK6E','oMbL_7lpD1w']
batch = service.new_batch_http_request(callback=insert_video)
for videoId in playlistItemsList:
batch.add(
service.playlistItems().insert(part="snippet",
body={"snippet": {
"playlistId": "PLs7sB4wt2IftzifT5G4cdjQlXZQpEk2Go",
"position": 0,
"resourceId": {"kind": "youtube#video", "videoId": videoId},
}
},
),
)
request = batch.execute()
Using the callback function I can see that the failed attempts are returning:
<HttpError 500 when requesting https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet&alt=json returned "Internal error encountered.". Details: "Internal error encountered.">
{
"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"errors": [
{
"message": "The request is missing a valid API key.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
}
Is there a way to sequentially add to playlist but utilising a batch method?
I noticed a serialise batch request within Google API docs - could this be what I'm looking for?
https://googleapis.github.io/google-api-python-client/docs/epy/googleapiclient.http.BatchHttpRequest-class.html#_execute
My code is based on this:
https://learndataanalysis.org/copy-videos-from-any-youtube-playlist-to-your-own-playlist-with-python-and-youtube-api/
question from:
https://stackoverflow.com/questions/65927024/youtube-api-v3-python-how-to-batch-insert-playlistitems