开发者

Searching YouTube with the API for .NET

开发者 https://www.devze.com 2023-04-08 11:50 出处:网络
I\'m trying to use the YouTube API to search YouTube with a search text. The sample code is as follows.

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

usi开发者_JS百科ng 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?


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

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号