YouTube的搜索与.NET的APIYouTube、NET、API

2023-09-03 17:14:58 作者:20.孤独与我

我想使用YouTube API来搜索YouTube与搜索文字。样品code是如下:

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

我的问题是, query.Query 要求 printVideoFeed 不存在 - ?我该如何使用API​​来搜索YouTube

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

推荐答案

虽然可以使用的。YouTube的 NET客户端库,我发现.NET API滞后正在取得的进展落后(比如,我不知道,如果你甚至可以得到的喜欢/不喜欢在协议本身的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.

相反,我建议您使用数据API协议,它使用HTTP和XML(在 ATOM格式)的。 NET有一个可以方便地使用/解析类。该文档也很齐全,并撰写查询是很容易的。

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

这将随后返回结构是这样的(尽管数据可能会有所不同,因为我以为小狗正在上传所有的时间新视频)的XML文档:

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/&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不暴露值。

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.

 
精彩推荐
图片推荐