Dies hat sich in Version 3 der YouTube-API geändert. Es gibt einen neuen Endpunkt namens commentThreads/list
, mit dem Sie einen Kommentarfaden für eine Ressource zurückgeben können.
Wenn Sie eine Liste mit Kommentaren zu einer Videoressource zurückgeben möchten, richten Sie eine GET-Anforderung mit part=id,snippet
und videoId=[VIDEO_ID]
ein. Ich werde https://www.youtube.com/watch?v=HwNIDcwfRLY als Beispiel werden:
HTTP GET https://www.googleapis.com/youtube/v3/commentThreads?part=id%2Csnippet&videoId=HwNIDcwfRLY&key={YOUR_API_KEY}
Lassen Sie uns den ersten Kommentar verwenden als Beispiel zurückgegeben:
{
"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
}
}
Beachten Sie, dass der Kommentar nicht wirklich in diesem topLevelComment
Objekt ist. textDisplay
gibt die leere Zeichenfolge zurück, bei der es sich um eine known issue mit der YouTube-API handelt. Wir brauchen eine zusätzliche Anforderung an commentThreads/list
mit id=[COMMENT_ID]
zu machen, wo [COMMENT_ID]
ist topLevelComment.id
:
HTTP GET https://www.googleapis.com/youtube/v3/commentThreads?part=id%2Csnippet&id=z120sfshyxzewt1nx23sevyr1vu1jd2pr04&key={YOUR_API_KEY}
Die resultierende Antwort des snippet
Wörterbuch wird der Kommentar des Benutzers als Standardwert für die textDisplay
Schlüssel:
"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"
}
}
Die Kommentar ist: "Mein nächster Klingelton! Yeah Boy!"
Beachten Sie, dass Sie auch eine Liste von bis zu 50 durch Kommas getrennten id
oder videoId
Zeichenfolgen von Kommentarobjekten übergeben können, die pro API-Aufruf abgerufen werden sollen.
Weitere Informationen und Beispielcode finden Sie im Handbuch Retrieve comments for a video.