contentDetails or duration not coming using Youtube v3 api

AtanuCSE picture AtanuCSE · Dec 10, 2014 · Viewed 16.4k times · Source

Look at this link , there is an example given

https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=YOUR_API_KEY
 &part=snippet,contentDetails,statistics,status

Part of the response is

"contentDetails": {
    "duration": "PT15M51S",
    "aspectRatio": "RATIO_16_9"
   },

Now I want to retrieve contentDetails or mainly duration. So I called using

https://www.googleapis.com/youtube/v3/search?part=snippet,contentDetails&key=[API_KEY]&q=something&maxResults=15&&fields=items,nextPageToken,prevPageToken,tokenPagination

It shows

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

Why? What am I missing? How to retrieve the duration for videos?

Answer

johnh10 picture johnh10 · Dec 11, 2014

As you've already found out, the Search:list call does not support contentDetails for the part parameter.

The part names that you can include in the parameter value for Search:list are id and snippet, and those return very little data. We're supposed to use that very little data from a search if we want to get more specific data on a video or videos.

So, to get a video duration when doing a search, you'll have to make a call like

GET https://www.googleapis.com/youtube/v3/search?part=id&q=anything&key={YOUR_API_KEY}

and extract the videoId from the response items

"id": {
"kind": "youtube#video",
"videoId": "5hzgS9s-tE8"
}

and use that to make the Videos:list call to get more specific data

https://www.googleapis.com/youtube/v3/videos?id=5hzgS9s-tE8&key=YOUR_API_KEY&part=snippet,contentDetails,statistics,status

and extract the duration from the response data

 "contentDetails": {
 "duration": "PT15M51S",
 "aspectRatio": "RATIO_16_9"
 },