我正在尝试使用YouTube API来搜索带有搜索文本的YouTube。示例代码如下。
using Google.YouTube;
using Google.GData.YouTube;
using Google.GData.Client;
using Google.GData.Extensions;
(..)
YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);
//order results by the number of views (most viewed first)
query.OrderBy = "viewCount";
// search for puppies and include restricted content in the search results
// query.SafeSearch could also be set to YouTubeQuery.SafeSearchValues.Moderate
query.Query = "puppy";
query.SafeSearch = YouTubeQuery.SafeSearchValues.None;
Feed<Video> videoFeed = request.Get<Video>(query);
printVideoFeed(videoFeed);我的问题是query.Query、request和printVideoFeed都不存在--我该如何使用这个接口来搜索YouTube?
发布于 2011-09-27 21:49:10
虽然您可以使用.NET client library for YouTube,但我发现API在协议本身方面落后于正在进行的开发(例如,我甚至不确定您是否可以从.NET中获得喜欢/不喜欢的信息)。
相反,我建议您使用Data API Protocol,它使用超文本传输协议和可扩展标记语言(在ATOM format中),.NET具有易于使用/解析的类。文档也非常完整,编写您的查询将非常容易。
在您的示例中,查询的URL为:
http://gdata.youtube.com/feeds/api/videos?v=2&orderby=viewCount&safeSearch=none&q=puppy
它随后将返回一个结构如下的XML文档(尽管数据可能会有所不同,因为我假设一直在上传小狗的新视频):
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom'
xmlns:app='http://www.w3.org/2007/app'
xmlns:media='http://search.yahoo.com/mrss/'
xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/'
xmlns:gd='http://schemas.google.com/g/2005'
xmlns:gml='http://www.opengis.net/gml'
xmlns:yt='http://gdata.youtube.com/schemas/2007'
xmlns:georss='http://www.georss.org/georss'
gd:etag='W/"C0cBR38zfCp7I2A9WhdUEU4."'>
<id>tag:youtube.com,2008:videos</id>
<updated>2011-09-27T13:44:16.184Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://gdata.youtube.com/schemas/2007#video'/>
<title>YouTube Videos matching query: puppy</title>
<logo>http://www.youtube.com/img/pic_youtubelogo_123x63.gif</logo>
<link rel='alternate' type='text/html' href='http://www.youtube.com'/>
...
<entry gd:etag='W/"CEINR347eCp7I2A9WhdUEEQ."'>
<id>tag:youtube.com,2008:video:vkeETehk8C8</id>
<published>2007-05-21T02:02:00.000Z</published>
<updated>2011-09-27T03:03:16.000Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://gdata.youtube.com/schemas/2007#video'/>
...如果您想充分利用已有的对象模型,还可以获取XML并将其放入YouTube .NET客户机结构中,以便于访问(虽然不容易,但这是可能的),但是向下拉到XML以获取API不公开的值。
发布于 2012-07-15 22:01:11
您要查找的内容在Authentication chapter of their .NET guide中。
基本上,您需要在开头添加以下内容:
YouTubeRequestSettings settings = new YouTubeRequestSettings("example app", clientID, developerKey);
YouTubeRequest request = new YouTubeRequest(settings);printVideoFeed方法只是一个打印所有元数据的演示,但是您可以在in the guide too中找到它。您可能想要对您得到的Feed做一些其他的事情。
不过,query.Query不应该被忽略。
https://stackoverflow.com/questions/7567669
复制相似问题