I need to be able to "like" a specific video by ID through the new YouTube API v3 for an authenticated user. I am following the activities/insert guide found here:
https://developers.google.com/youtube/v3/docs/activities/insert
This example code runs fine for posting a bulletin to my channel but when I try to modify the body to form a like statement, I keep getting a 400 error. Here is what Ive changed from the original example where the body dict is setup:
body = {}
body["snippet"] = dict(type='like')
body["contentDetails"] = dict(
like=dict(
resourceId=dict(
kind="youtube#video",
videoId='_M9khs87xQ8'
)
)
)
According to the following documentation, the fields seem to be setup correctly.
https://developers.google.com/youtube/v3/docs/activities
But I keep getting a 400 HttpEror like so
<HttpError 400 when requesting https://www.googleapis.com/youtube/v3/activities?alt=json&part=snippet%2CcontentDetails returned "Bad Request">
Ive also tried adapting this to favorite a video action but get the same result. Am I missing some of the required fields? Is this the correct endpoint for creating a like action?
Thanks in advance, Justin
Update
This issue has been answered by Jeff and the working solution is posted below
for item in youtube.channels().list(part='contentDetails', mine=True).execute().get('items', []):
playlists = item['contentDetails'].get('relatedPlaylists', {})
if 'likes' in playlists:
body = {
"snippet": {
"playlistId": playlists['likes'],
"resourceId": {
"kind": 'youtube#video',
"videoId": '_M9khs87xQ8'
}
}
}
youtube.playlistItems().insert(body=body, part='snippet').execute()
To "like" a video in v3, you need to add it to a specific playlist id. (You can also read this playlist to get a list of videos that you've previously "like"d.)
The proper call to make is a
playlistItems.insert()
(i.e. a POST tohttps://www.googleapis.com/youtube/v3/playlistItems
) with the following request body:The two things to plug in there are
LIKED_LIST_ID
andVIDEO_ID
.VIDEO_ID
should hopefully be self-explanatory.LIKED_LIST_ID
corresponds to the playlist id you get back when making a channels.list(part=contentDetails) request. The response looks likeYou can plug in some of those other playlist ids to, for instance, add a video as a favorite or add it to the watch later list for an account. The code would be identical to the code for "like"ing a video.