c# - 使用适用于 .NET 的 API 搜索 YouTube

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

我正在尝试使用 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?

最佳答案

虽然您可以使用 .NET client library for YouTube ,我发现 .NET API 落后于协议(protocol)本身的开发(例如,我不确定您是否甚至可以从 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 客户端结构中以便于访问(虽然不容易,但这是可能的),但下拉到用于获取 API 未公开的值的 XML。

关于c# - 使用适用于 .NET 的 API 搜索 YouTube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7567669/

相关文章:

.net - 枚举是引用类型还是值类型?

c# - 错误 0x80005000 和 DirectoryServices

javascript - google api javascript 登录用户的电子邮件

javascript - 需要用户凭据 : Google Cloud Print Submit API

c# - 如何在 Azure Service Fabric 应用程序中注入(inject) IHttpClientFactory?

c# - 如果在 using block 中调用 return,那么幕后操作的顺序是什么?

c# - 如何在 C# 中使用私钥? "Cannot find the requested object."

c# - 为什么我不能使用索引器将项目添加到通用列表?

.net - Thread.CurrentThread.CurrentUICulture 和 CultureInfo.CurrentUICulture 是否始终同步?

python - 如何获取 Google Books API 中的类别列表?