【问题标题】:YouTube API Comments FeedYouTube API 评论供稿
【发布时间】:2010-12-03 19:56:33
【问题描述】:

我正在尝试使用 YouTube API for .NET 从视频条目中获取评论源。我正在使用 WPF 和 C# 编写一个程序,但我似乎一辈子都无法弄清楚如何检索此提要。

我尝试查看YouTube API Developer's Guide,但它似乎缺少有关评论源的一些信息(靠近页面底部)。

【问题讨论】:

    标签: c# wpf youtube youtube-api youtube-data-api


    【解决方案1】:
    YouTubeRequest request = ... // Your request object    
    Video v = ... // Your video object
    Feed<Comment> comments = request.GetComments(v);
    

    comments.entries 将包含视频 v 的所有 cmets 作为 Comment 对象,因此您根本不需要弄乱提要。

    【讨论】:

      【解决方案2】:

      这在 YouTube API 的第 3 版中有所更改。有一个名为 commentThreads/list 的新端点,它允许您为资源返回一个 cmets 线程。

      如果要返回视频资源的 cmets 列表,请使用 part=id,snippetvideoId=[VIDEO_ID] 设置 GET 请求。我将以https://www.youtube.com/watch?v=HwNIDcwfRLY 为例:

      HTTP GET https://www.googleapis.com/youtube/v3/commentThreads?part=id%2Csnippet&videoId=HwNIDcwfRLY&key={YOUR_API_KEY}
      

      我们以返回的第一条评论为例:

      {
          "kind": "youtube#commentThread",
          "etag": "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/jhK_kJqnNF8_fiRI_o7w6ehubv8\"",
          "id": "z120sfshyxzewt1nx23sevyr1vu1jd2pr04",
          "snippet": {
              "videoId": "HwNIDcwfRLY",
              "topLevelComment": {
                  "kind": "youtube#comment",
                  "etag": "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/h903NemnXx-8Hfe6lRIYCFERSe4\"",
                  "id": "z120sfshyxzewt1nx23sevyr1vu1jd2pr04",
                  "snippet": {
                  "authorDisplayName": "mach-a-chine seahawksgoonie",
                  "authorProfileImageUrl": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
                  "authorChannelUrl": "http://www.youtube.com/channel/UCBmJ0sw7plIZHLvhfz7oo_w",
                  "authorChannelId": {
                      "value": "UCBmJ0sw7plIZHLvhfz7oo_w"
                  },
                  "videoId": "HwNIDcwfRLY",
                  "textDisplay": "",
                  "authorGoogleplusProfileUrl": "https://plus.google.com/102274783439566633837",
                  "canRate": true,
                  "viewerRating": "none",
                  "likeCount": 0,
                  "publishedAt": "2016-02-05T03:42:35.158Z",
                  "updatedAt": "2016-02-05T03:42:35.158Z"
                  }
              },
              "canReply": true,
              "totalReplyCount": 0,
              "isPublic": true
          }
      }
      

      请注意,注释实际上不在此 topLevelComment 对象中。 textDisplay 返回空字符串,即 YouTube API 的 known issue。我们需要使用id=[COMMENT_ID]commentThreads/list 发出额外请求,其中[COMMENT_ID]topLevelComment.id

      HTTP GET https://www.googleapis.com/youtube/v3/commentThreads?part=id%2Csnippet&id=z120sfshyxzewt1nx23sevyr1vu1jd2pr04&key={YOUR_API_KEY}
      

      结果响应的snippet 字典将用户的评论作为textDisplay 键的值:

      "snippet": {
          "authorDisplayName": "mach-a-chine seahawksgoonie",
          "authorProfileImageUrl": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
          "authorChannelUrl": "http://www.youtube.com/channel/UCBmJ0sw7plIZHLvhfz7oo_w",
          "authorChannelId": {
                  "value": "UCBmJ0sw7plIZHLvhfz7oo_w"
          },
          "videoId": "HwNIDcwfRLY",
          "textDisplay": "my next ring tone! yeah boy!\ufeff",
          "authorGoogleplusProfileUrl": "https://plus.google.com/102274783439566633837",
          "canRate": true,
          "viewerRating": "none",
          "likeCount": 0,
          "publishedAt": "2016-02-05T03:42:35.158Z",
          "updatedAt": "2016-02-05T03:42:35.158Z"
          }
      }
      

      评论是:“我的下一个铃声!是的男孩!”

      请注意,您还可以传入最多 50 个以逗号分隔的 idvideoId 评论对象字符串的列表,以在每个 API 调用中检索。

      有关更多信息和示例代码,请参阅Retrieve comments for a video 指南。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-07
        • 2012-11-05
        • 1970-01-01
        • 2015-08-23
        • 2013-11-21
        • 1970-01-01
        • 1970-01-01
        • 2018-04-14
        相关资源
        最近更新 更多