So I've been trying to generate YouTube playlists with the corresponding video IDs, but when I try to do it in batches of 50, I get this error: "Invalid JSON payload received. Unknown name "": Root element must be a message." Details: "[{'message': 'Invalid JSON payload received. Unknown name "": Root element must be a message.', 'reason': 'invalid'}]"
def add_songs_to_playlist(playlist_id, video_ids): for i in range(0, len(video_ids), 50): chunk = video_ids[i:i+50] video_objs = [] for video_id in chunk: video_objs.append({"snippet": {"playlistId": playlist_id,"resourceId": {"kind": "youtube#video","videoId": video_id } } }) try: youtube.playlistItems().insert( part="snippet", body=json.dumps(video_objs) ).execute() except googleapiclient.errors.HttpError as e: error_json = json.loads(e.content) if error_json["error"]["errors"][0]["reason"] == "videoNotFound": print(f"Video with ID {video_id} not found.") else: print(f"Error adding videos to playlist: {e}")
If I do separate API calls for each insert, it works, but that's not ideal because it uses up all my quotas. I made sure all the video IDs were valid. I also used jsonlint to make sure the JSON payload was valid, and it was valid. I have been trying everything so if anyone can help, please let me know.