You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过YouTube API实现视频评论的置顶功能

如何通过YouTube API将评论设置为置顶状态

你目前的代码已经能成功添加评论,但要实现置顶,需要在插入评论后调用commentThreads.update接口来设置置顶属性——只有视频的所有者账号才能执行这个操作,同时你的API请求需要包含https://www.googleapis.com/auth/youtube.force-ssl权限范围。

具体步骤如下:

  • 先执行你现有的代码插入评论,获取返回的评论线程ID
  • 创建更新用的CommentThread对象,指定snippet.isPinned为true
  • 调用update接口完成置顶

修改后的完整代码如下:

// 原有的评论插入代码
$commentSnippet = new Google_Service_YouTube_CommentSnippet();
$commentSnippet->setTextOriginal('My Comment');
$topLevelComment = new Google_Service_YouTube_Comment();
$topLevelComment->setSnippet($commentSnippet);
$commentThreadSnippet = new Google_Service_YouTube_CommentThreadSnippet();
$commentThreadSnippet->setTopLevelComment($topLevelComment);
$commentThread = new Google_Service_YouTube_CommentThread();
$commentThread->setSnippet($commentThreadSnippet);
$commentThreadSnippet->setVideoId($videoId);
$videoCommentInsertResponse = $youtube->commentThreads->insert('snippet', $commentThread);

// 新增的置顶操作
$updatedCommentThread = new Google_Service_YouTube_CommentThread();
$updatedSnippet = new Google_Service_YouTube_CommentThreadSnippet();
// 设置要置顶的评论线程ID
$updatedCommentThread->setId($videoCommentInsertResponse->getId());
// 标记为置顶
$updatedSnippet->setIsPinned(true);
// 关联视频ID(必须和原评论一致)
$updatedSnippet->setVideoId($videoId);
// 关联原顶级评论
$updatedSnippet->setTopLevelComment($videoCommentInsertResponse->getSnippet()->getTopLevelComment());
$updatedCommentThread->setSnippet($updatedSnippet);

// 调用update接口,注意要指定'snippet'参数
$youtube->commentThreads->update('snippet', $updatedCommentThread);

注意事项:

  • 确保你的API客户端已获得youtube.force-ssl权限
  • 操作账号必须是目标视频的所有者,否则会返回权限错误
  • 每个视频只能有一条置顶评论,新的置顶操作会替换原有的置顶评论

内容的提问来源于stack exchange,提问作者Prinple Vrlo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 10:57:34