YouTube API v3: Liking a video in Python

2019-03-31 12:59发布

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()

1条回答
爷、活的狠高调
2楼-- · 2019-03-31 13:29

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 to https://www.googleapis.com/youtube/v3/playlistItems) with the following request body:

"body": {
  "snippet": {
    "playlistId": LIKED_LIST_ID,
    "resourceId": {
      "kind": "youtube#video",
      "videoId": VIDEO_ID
    }
  }
}

The two things to plug in there are LIKED_LIST_ID and VIDEO_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 like

"contentDetails": {
  "relatedPlaylists": {
    "likes": "LL0c49w3rVoFjTkQVbyRs8Sg",
    "favorites": "FL0c49w3rVoFjTkQVbyRs8Sg",
    "uploads": "UU0c49w3rVoFjTkQVbyRs8Sg",
    "watchHistory": "HL0c49w3rVoFjTkQVbyRs8Sg",
    "watchLater": "WL0c49w3rVoFjTkQVbyRs8Sg"
  }
}

You 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.

查看更多
登录 后发表回答