使用 .NET API 搜索 YouTube

发布于 2024-12-06 14:20:02 字数 794 浏览 1 评论 0 原文

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

I'm trying to use the YouTube API to search YouTube with a search text. The sample code is as follows.

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);

My issue is that query.Query, request and printVideoFeed don't exist - how do I use the API to search YouTube?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦太阳 2024-12-13 14:20:02

虽然您可以使用YouTube .NET 客户端库,但我发现.NET API 落后于协议本身的开发(例如,我不确定您是否还可以从 API 中获取喜欢/不喜欢的信息)。

相反,我建议您使用数据 API 协议,它使用 HTTP 和 XML(采用 ATOM 格式),.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 未公开的值。

While you can use the .NET client library for YouTube, I find that the .NET API lags behind the developments that are being made (for example, I'm not sure if you can even get the like/dislike information from the API yet) in the protocol itself.

Instead, I'd recommend that you use the Data API Protocol, it uses HTTP and XML (in ATOM format) which .NET has classes that can easily use/parse. The documentation is also very complete, and composing your queries would be quite easy.

In your example, the URL for your query would be:

http://gdata.youtube.com/feeds/api/videos?v=2&orderby=viewCount&safeSearch=none&q=puppy

Which would subsequently return an XML document structured like this (although the data might be different, as I assume new videos of puppies are being uploaded all the time):

<?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'/>
...

You can also get the XML and put it into the YouTube .NET client structures for easy access (although not easily, it is possible) if you want to capitalize on the object models that they already have, but drop down to the XML to get values that the API doesn't expose.

删除会话 2024-12-13 14:20:02

您要查找的内容位于其 .NET 指南的身份验证章节

基本上,您需要在开头添加此内容:

YouTubeRequestSettings settings = new YouTubeRequestSettings("example app", clientID, developerKey);
YouTubeRequest request = new YouTubeRequest(settings);

printVideoFeed 方法只是打印所有元数据的演示,但您可以在 也在指南中。您可能想用获得的 Feed 做其他事情。

不过 query.Query 不应该丢失。

What you're looking for is in the Authentication chapter of their .NET guide.

Basically, you need to add this in the beginning:

YouTubeRequestSettings settings = new YouTubeRequestSettings("example app", clientID, developerKey);
YouTubeRequest request = new YouTubeRequest(settings);

The printVideoFeed method is just a demo to print out all the meta-data, but you can find it in the guide too. You probably want to do something else with the Feed you get.

query.Query shouldn't be missing though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文