0

I am using this to get the information of the video such title and image.

var urlString = "https://www.googleapis.com/youtube/v3/search?part=snippet&fields=items(id,snippet(title,channelTitle,thumbnails))&order=viewCount&q=\(text)&type=video&maxResults=25&key=\(apiKey)"

This above URL will return a JSON.

     ["items": <__NSArrayI 0x1c41b03e0>(
{
    id =     {
        kind = "youtube#video";
        videoId = n3T3K2MHNjM;
    };
    snippet =     {
        channelTitle = "Ty Dolla $ign";
        thumbnails =         {
            default =             {
                height = 90;
                url = "https://i.ytimg.com/vi/n3T3K2MHNjM/default.jpg";
                width = 120;
            };
            high =             {
                height = 360;
                url = "https://i.ytimg.com/vi/n3T3K2MHNjM/hqdefault.jpg";
                width = 480;
            };
            medium =             {
                height = 180;
                url = "https://i.ytimg.com/vi/n3T3K2MHNjM/mqdefault.jpg";
                width = 320;
            };
        };
        title = "Ty Dolla $ign & Future - Darkside feat. Kiiara (from Bright: The Album) [Official Audio]";
    };
},

but there is no content details in the JSON I would like to access the content details since its contains the duration of the video which i needed. I had try to access with url like this by inserting the contentDetails into the URL but it is not working.

var urlString = "https://www.googleapis.com/youtube/v3/search?part=snippet&fields=items(id,contentDetails,snippet(title,channelTitle,thumbnails))&order=viewCount&q=\(text)&type=video&maxResults=25&key=\(apiKey)"

How can i retrieve the content details also ?

Mannopson
  • 2,634
  • 1
  • 16
  • 32

1 Answers1

0

The search API v3 does not have the duration in the search API , you need use the video API to get the video information.

var urlString = "https://www.googleapis.com/youtube/v3/videos?key=\(apiKey)&part=contentDetails&id="

Use this URL and then add in the id behind like this U4_Os5LNVjg, s23mnsidmm for multiple ID and then use this to convert the duration format from YouTube How to Convert duration form youtube api in swift?