如何获取 YouTube 的“活动”标签下列出的视频?
尝试使用以下方法获取:
要搜索实时事件,您需要设置live
为eventType
键video
的值和键的值type
。
您的请求将如下所示:
HTTP GET: https://www.googleapis.com/youtube/v3/search?eventType=live&part=id%2C+snippet&type=video&key={YOUR_API_KEY}
目标-C:
// Set up your URL
NSString *youtubeApi = @"https://www.googleapis.com/youtube/v3/search?eventType=live&part=id%2C+snippet&type=video&key={YOUR_API_KEY}";
NSURL *url = [[NSURL alloc] initWithString:youtubeApi];
// Create your request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// Send the request asynchronously
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// Callback, parse the data and check for errors
if (data && !connectionError) {
NSError *jsonError;
NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
if (!jsonError) {
NSLog(@"Response: %@", jsonResult);
}
}
}];
迅速:
// Set up your URL
let youtubeApi: String = "https://www.googleapis.com/youtube/v3/search?eventType=live&part=id%2C+snippet&type=video&key={YOUR_API_KEY}"
var url: NSURL = NSURL(string: youtubeApi)!
// Create your request
var request: NSURLRequest = NSURLRequest(URL: url)
let queue:NSOperationQueue = NSOperationQueue()
// Send the request asynchronously
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
// Callback, parse the data and check for errors
if data != nil && error == nil {
var err: NSError?
if let jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary {
println("Response: \(jsonResult)")
}
} else {
println("Error: \(error)")
}
})
回复:
{
"kind": "youtube#searchListResponse",
"etag": "\"Y3xTLFF3RLtHXX85JBgzzgp2Enw/zpkE9CJT5UE3J3ftgSZ5oGJd9mI\"",
"nextPageToken": "CAUQAA",
"pageInfo": {
"totalResults": 3011,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"Y3xTLFF3RLtHXX85JBgzzgp2Enw/eGgYtvB5TXQD0T0QJiElo89I6Sw\"",
"id": {
"kind": "youtube#video",
"videoId": "B9zFbRYW_sk"
},
"snippet": {
"publishedAt": "2015-05-17T02:25:30.000Z",
"channelId": "UC5BMQOsAB8hKUyHu9KI6yig",
"title": "[On-Air] KBS World Latino",
"description": "KBS World Latino ha sido lanzado oficialmente el 1 de noviembre! ¡En este canal, se transmitirán los doramas, shows de música y entreteniemnto más ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/B9zFbRYW_sk/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/B9zFbRYW_sk/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/B9zFbRYW_sk/hqdefault.jpg"
}
},
"channelTitle": "kbsworld",
"liveBroadcastContent": "live"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"Y3xTLFF3RLtHXX85JBgzzgp2Enw/lRgnr-IYRKQ7dpUwvBDfpX0Qz3Q\"",
"id": {
"kind": "youtube#video",
"videoId": "8HxN3ZHMbjc"
},
"snippet": {
"publishedAt": "2015-01-02T05:10:04.000Z",
"channelId": "UC5BMQOsAB8hKUyHu9KI6yig",
"title": "[On-Air] KBS World 24",
"description": "700만 해외동포와 함께 하는 KBS World 24!!! 이제 해외에서도 KBS의 실시간 뉴스를 고화질의 KBS World 24채널로 시청하실 수 있습니다. KBS World 24 채널은...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/8HxN3ZHMbjc/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/8HxN3ZHMbjc/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/8HxN3ZHMbjc/hqdefault.jpg"
}
},
"channelTitle": "kbsworld",
"liveBroadcastContent": "live"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"Y3xTLFF3RLtHXX85JBgzzgp2Enw/exCIHag5T6lQhpLHOOPZ9Z63lKs\"",
"id": {
"kind": "youtube#video",
"videoId": "wyMOYHiatos"
},
"snippet": {
"publishedAt": "2015-05-28T13:50:09.000Z",
"channelId": "UCLA_DiR1FfKNvjuUpBHmylQ",
"title": "NASA HDTV",
"description": "NASA TV airs a variety of regularly scheduled, pre-recorded educational and public relations programming 24 hours a day on its various channels. Programs ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/wyMOYHiatos/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/wyMOYHiatos/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/wyMOYHiatos/hqdefault.jpg"
}
},
"channelTitle": "NASAtelevision",
"liveBroadcastContent": "live"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"Y3xTLFF3RLtHXX85JBgzzgp2Enw/C_dxvfEwfJ1mGWVzbJAVSNHvdpk\"",
"id": {
"kind": "youtube#video",
"videoId": "kRonNPz4MJw"
},
"snippet": {
"publishedAt": "2015-04-30T21:07:32.000Z",
"channelId": "UCzv6uVYjfvE8X-_F3cicWog",
"title": "إذاعة القرآن الكريم من القاهرة - بث مباشر - Quran Karim",
"description": "إذاعة القرآن الكريم من القاهرة - بث مباشر - Quran Karim *** هذه الصفحه غير رسميه *** يمكنكم التواصل مع اذاعه القران...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/kRonNPz4MJw/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/kRonNPz4MJw/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/kRonNPz4MJw/hqdefault.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "live"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"Y3xTLFF3RLtHXX85JBgzzgp2Enw/DJJYpR9kef3SlbZnsbVcmqDp6Qs\"",
"id": {
"kind": "youtube#video",
"videoId": "9SdaRmrVBZ4"
},
"snippet": {
"publishedAt": "2015-04-07T00:15:03.000Z",
"channelId": "UC8NnosPOvXnm0O1u5YnLQiw",
"title": "Long-Eared Owl powered by EXPLORE.org",
"description": "Love Birds Subscribe http://goo.gl/zqy2fE - Watch the Best Moments https://goo.gl/FqdepR Multicam Experience http://goo.gl/PA33mO Facebook ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/9SdaRmrVBZ4/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/9SdaRmrVBZ4/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/9SdaRmrVBZ4/hqdefault.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "live"
}
}
]
}