Youtube REST API v3 - include statistics for video

2019-04-05 05:09发布

问题:

I want to perform search queries using Youtube API v3. What I need is to retrieve video ids and statistics for each video. From the docs I can see that statistics is not returned for video items. If I try to ask for statistics using this query:

https://www.googleapis.com/youtube/v3/search?type=video&part=snippet,statistics&q=kittens&key={MY_KEY}

I receive an error:

{
 "error": {
  "errors": [
   {
    "domain": "youtube.part",
    "reason": "unknownPart",
    "message": "statistics",
    "locationType": "parameter",
    "location": "part"
   }
  ],
  "code": 400,
  "message": "statistics"
 }
}

So I guess that I need to make two requests:

  1. Perform actual search and retrieve list of video ids.
  2. Make API request https://developers.google.com/youtube/v3/docs/videos/list to retrieve statistics for each video.

Or maybe I'm missing something and there's a way to get statistics for videos within one search query?

回答1:

In the guide, they specify "the part names that you can include in the parameter value are id and snippet" when using https://www.googleapis.com/youtube/v3/search. (statistics is not an accepted value).

So I think that you have to make two requests as you say, at least that is what I'm doing. I couldn't find any other solution. I would be interested to know if there was a workaround...



回答2:

To avoid redundancy of data returned and not to use bandwidth with extra data, "video search data" and "video statistics" data are decoupled in API. You are right about two calls. In general, to get faster response, only use the "part" s in request that you will use in your application.