【问题标题】:Searching YouTube with the API for .NET使用 API for .NET 搜索 YouTube
【发布时间】:2011-11-25 22:18:40
【问题描述】:

我正在尝试使用 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.QueryrequestprintVideoFeed 不存在 - 如何使用 API 搜索 YouTube?

【问题讨论】:

    标签: c# .net google-api youtube-api


    【解决方案1】:

    虽然您可以使用 .NET client library for YouTube,但我发现 .NET API 落后于正在进行的开发(例如,我不确定您是否甚至可以从 API 中获取喜欢/不喜欢的信息) 在协议本身中。

    相反,我建议您使用Data API Protocol,它使用HTTP 和XML(在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/&quot;C0cBR38zfCp7I2A9WhdUEU4.&quot;'>
        <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/&quot;CEINR347eCp7I2A9WhdUEEQ.&quot;'>
            <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 未公开的值。

    【讨论】:

    • 还是这样吗? Youtube API C# 库是否更接近有用的东西?我不想认为我必须进行 XML 解析...
    • @Farinha YouTube API (v3) 实际上都是基于 JSON 的,所以当它上线时,这个答案必须随之更新(它仍然处于测试阶段,但我更愿意解析JSON 在他们的库上,因为他们不提供任何异步调用,这对我来说是一个真正的杀手)。
    【解决方案2】:

    您要查找的内容在 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 不应该丢失。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 2020-02-22
      • 2011-09-14
      • 2014-10-23
      • 2017-06-21
      • 1970-01-01
      相关资源
      最近更新 更多