YouTube API v3:在 Python 中点赞视频 [英] YouTube API v3: Liking a video in Python

查看:25
本文介绍了YouTube API v3:在 Python 中点赞视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够通过新的 YouTube API v3 为经过身份验证的用户按 ID喜欢"特定视频.我正在关注此处的活动/插入指南:

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/活动/插入

此示例代码在向我的频道发布公告时运行良好,但是当我尝试修改正文以形成类似语句时,我不断收到 400 错误.这是我对设置 body dict 的原始示例所做的更改:

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

但我一直收到这样的 400 HttpEror

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?

提前致谢,贾斯汀

Jeff 已经回答了这个问题,下面发布了工作解决方案

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

推荐答案

要喜欢" v3 中的视频,您需要将其添加到特定的播放列表 ID.(您也可以阅读此播放列表以获取您之前喜欢"的视频列表.)

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

正确的调用方式是 playlistItems.insert()(即 POST 到 https://www.googleapis.com/youtube/v3/playlistItems)具有以下请求正文:

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
    }
  }
}

要插入的两件事是LIKED_LIST_IDVIDEO_ID.VIDEO_ID 应该是不言自明的.LIKED_LIST_ID 对应于您在发出 channels.list(part=contentDetails) 请求时返回的播放列表 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"
  }
}

您可以插入一些其他播放列表 ID,例如,将视频添加为收藏或将其添加到帐户的稍后观看列表中.该代码将与喜欢"视频的代码相同.

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.

这篇关于YouTube API v3:在 Python 中点赞视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆