2009-10-05 106 views
6

我試圖得到使用.NET在YouTube API視頻條目訂閱評論。我正在研究一個WPF和C#中的程序,但似乎無法爲我的生活弄清楚如何檢索這個提要。的YouTube API評論Feed

我試着看YouTube API Developer's Guide,但似乎缺少有關評論飼料的一些信息(靠近頁面底部)。

回答

-1
YouTubeRequest request = ... // Your request object  
Video v = ... // Your video object 
Feed<Comment> comments = request.GetComments(v); 

comments.entries將包含視頻V作爲Comment對象所有的評論,所以你不需要亂用料都沒有。

1

這在YouTube API第3版已經改變。有一個叫commentThreads/list新的端點,它允許你返回評論的線程的資源。

如果你想在視頻資源返回的評論列表,建立一個GET請求與part=id,snippetvideoId=[VIDEO_ID]。我將使用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返回空字符串,這是一個known issue使用YouTube API。我們需要一個額外的請求commentThreads/listid=[COMMENT_ID],其中[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指南的附加信息和示例代碼。