我想知道如何在iOS应用中使用新的YouTube API(第3版),但我不知道如何做.
我做了很多关于它的研究,但是我发现所有的例子和老API的代码,所以它们是无效的.
现在,我明白了,使用新的API你必须在Google开发者控制台中创建一个项目(而且我已经做到了)…但是他们会将它们发送到一个带有一些代码的页面,但我真的不明白如何使用它. link to google api page
我需要知道的是如何从YouTube视频的给定网址中检索一些信息,我需要的信息是“喜欢”的总数和“视图”的总数…使用API 2很简单它…但现在我真的不知道从哪里开始…
有没有人可以用一些例子和一些代码解释如何实现这一点?
我很确定很多人会从中受益.
我做了很多关于它的研究,但是我发现所有的例子和老API的代码,所以它们是无效的.
现在,我明白了,使用新的API你必须在Google开发者控制台中创建一个项目(而且我已经做到了)…但是他们会将它们发送到一个带有一些代码的页面,但我真的不明白如何使用它. link to google api page
我需要知道的是如何从YouTube视频的给定网址中检索一些信息,我需要的信息是“喜欢”的总数和“视图”的总数…使用API 2很简单它…但现在我真的不知道从哪里开始…
有没有人可以用一些例子和一些代码解释如何实现这一点?
我很确定很多人会从中受益.
解决方法
您不必使用Google提供的iOS客户端进行这些请求.
>导航到API Console并为您的iOS应用程序生成一个新的简单API访问键.确保在提供的窗口中输入您的应用程序的包标识符.或者,您可以创建一个Server API密钥,以便使用基本请求进行测试,并从命令行卷曲.
>根据您的需要查找相关终端.要查找有关视频的信息,您需要使用Videos.list方法.
首先,设置你的URL.我将使用此URL作为示例:https://www.youtube.com/watch?v=AKiiekaEHhI
您将要为part参数指定一个值.从您的问题,您似乎想要传递片段,contentDetails和统计值(虽然对于喜欢和视图,您只需要统计值).
然后传入您的视频的ID(在这种情况下,AKiiekaEHhI,最多可以添加50个逗号分隔的ID)和您的API密钥.您的网址应如下所示:
https://www.googleapis.com/youtube/v3/videos?part=contentDetails%2C+snippet%2C+statistics&id=AKiiekaEHhI&key={YOUR_API_KEY}
你也可以在API Explorer这样做.
Swift实施:
// Set up your URL
let youtubeApi = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails%2C+snippet%2C+statistics&id=AKiiekaEHhI&key={YOUR_API_KEY}"
let url = NSURL(string: youtubeApi)
// Create your request
let task = NSURLSession.sharedSession().dataTaskWithURL(url!,completionHandler: { (data,response,error) -> Void in
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!,options: NSJSONReadingOptions.AllowFragments) as? [String : AnyObject] {
print("Response from YouTube: \(jsonResult)")
}
}
catch {
print("json error: \(error)")
}
})
// Start the request
task.resume()
Objective-C实现:
(此帖子已编辑为支持NSURLSession.对于使用NSURLConnection的实现,请检查编辑历史记录)
// Set up your URL
Nsstring *youtubeApi = @"https://www.googleapis.com/youtube/v3/videos?part=contentDetails%2C+snippet%2C+statistics&id=AKiiekaEHhI&key={YOUR_API_KEY}";
NSURL *url = [[NSURL alloc] initWithString:youtubeApi];
// Create your request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// Send the request asynchronously
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,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 from YouTube: %@",jsonResult);
}
}
}] resume];
您的日志将如下所示:
Response from YouTube: {
etag = "\"NO6QTeg0-3ShswIeqLchQ_mzWJs/AAjIATmVK_8ySsAWwEuNfdZdjW4\"";
items = (
{
contentDetails = {
caption = false;
deFinition = hd;
dimension = 2d;
duration = PT17M30S;
licensedContent = 1;
};
etag = "\"NO6QTeg0-3ShswIeqLchQ_mzWJs/8v8ee5uPZQa1-ucVdjBdAVXzcZk\"";
id = AKiiekaEHhI;
kind = "youtube#video";
snippet = {
categoryId = 20;
channelId = UCkvdZX3SVgfDW8ghtP1L2Ug;
channelTitle = "Swordless Link";
description = "Follow me on Twitter! http://twitter.com/swordlesslink\n\nFollow me on TwitchTV for live video game streaming! http://twitch.tv/swordlesslink";
livebroadcastContent = none;
localized = {
description = "Follow me on Twitter! http://twitter.com/swordlesslink\n\nFollow me on TwitchTV for live video game streaming! http://twitch.tv/swordlesslink";
title = "The Legend of Zelda: Majora's Mask With Glitches - Part 17: Going Against the Flow";
};
publishedAt = "2015-05-04T10:01:43.000Z";
thumbnails = {
default = {
height = 90;
url = "https://i.ytimg.com/vi/AKiiekaEHhI/default.jpg";
width = 120;
};
high = {
height = 360;
url = "https://i.ytimg.com/vi/AKiiekaEHhI/hqdefault.jpg";
width = 480;
};
medium = {
height = 180;
url = "https://i.ytimg.com/vi/AKiiekaEHhI/mqdefault.jpg";
width = 320;
};
standard = {
height = 480;
url = "https://i.ytimg.com/vi/AKiiekaEHhI/sddefault.jpg";
width = 640;
};
};
title = "The Legend of Zelda: Majora's Mask With Glitches - Part 17: Going Against the Flow";
};
statistics = {
commentCount = 54;
dislikeCount = 3;
favoriteCount = 0;
likeCount = 265;
viewCount = 6356;
};
}
);
kind = "youtube#videoListResponse";
pageInfo = {
resultsPerPage = 1;
totalResults = 1;
};
} with error: nil
项目键的对象将是您传递给请求的每个视频ID的一系列信息.
通过挖掘这个回应,您将能够获得所需的信息.例如:
if let items = jsonResult["items"] as? [AnyObject]? {
println(items?[0]["statistics"])
}
会给你一个视频的统计数据的字典(你可以得到喜欢的数量和观看次数).
{
commentCount = 54;
dislikeCount = 3;
favoriteCount = 0;
likeCount = 265;
viewCount = 6356;
}
同样的方法可以用于现场直播.