Checking if video is age restricted with v3 of YouTube API

百般思念 提交于 2019-12-08 08:26:43

问题


Is there a way with v3 of the YouTube API to check if a video has an age restriction on it? Looking at the documentation I've been unable to confirm this.


回答1:


I found something related to this,check

 https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id={VIDEO_ID}&key={YOUR_API_KEY}

replace VIDEO_ID with the id of video you want to check.

API Response:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"mPrpS7Nrk6Ggi_P7VJ8-KsEOiIw/el3Y0P65UwM366CJD3POX-W4y0c\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {

   "kind": "youtube#video",
   "etag": "\"mPrpS7Nrk6Ggi_P7VJ8-KsEOiIw/Rn64PVwC0Uhr4xp41vDOqygjJ9c\"",
   "id": "VIDEO_ID",
   "contentDetails": {
    "duration": "PT10M6S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": false,
    "contentRating": {
     "ytRating": "ytAgeRestricted"
    }
   }
  }
 ]
}

Check the ytRating, it has value ytAgeRestricted, means the video is age restricted.

From Youtube-API docs:

contentDetails.contentRating.ytRating : string
       A rating that YouTube uses to identify age-restricted content.
       Valid values for this property are: ytAgeRestricted



回答2:


This is what I did:

var myWebClient = new WebClient();
var responseString = myWebClient.DownloadString(" https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=" + videoId + "&key=your_api_key_here");

var parsedResponseString = JObject.Parse(responseString);
JToken token = parsedResponseString["items"][0]["contentDetails"]["contentRating"];

if (token != null) {
  // there is no "contentRating" so no age restriction because "ytRating", if present, would be inside of "contentRating"

}


来源:https://stackoverflow.com/questions/33736103/checking-if-video-is-age-restricted-with-v3-of-youtube-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!