Youtube V3 API - Search videos by title

Asked

Viewed 191 times

3

Hello.

I’m using the Youtube V3 API to search for videos in a .net. project.I happen to have the exact title and need to search for it. In the documentation it is not clear if it is possible to search by title. API documentation

Today I have the approximate date, so I use the properties PublishedAfter and PublishedBefore, then I go through the list of results to search the video with the title I need. I would like to optimize my query, since I own the title of the video.

1 answer

0

Whereas you already have the Key API and it is enabled, in short:

var query = 'mutant giant spider dog'
gapi.client.load('youtube', 'v3', function() {
   gapi.client.setApiKey('[SUA_CHAVE_API]');

   var request = gapi.client.youtube.search.list({
        part: 'snippet',
        q: query,
        maxResults: 1
    });
    request.execute(function(response) {
       $.each(response.items, function(i, item) {
          var idVideo = item['id']['videoId'];
          var urlVideo = "https://www.youtube.com/embed/" + idVideo;
          //FAZ O QUE QUISER COM OS DADOS. title, description,..
       });
    });
});

I set maxResults to 1 to bring the most relevant video based on the name, so passing the full name must be the first one on the list. But if you want to iterate through the list increase the maxResult as in jsfiddler below.

Test on this Fiddler: http://jsfiddle.net/rodrigorf/2ta48oc9/

Note: remember to load the Jquery and API Google

============================================================

The query via C# is very similar too:

YoutubeService youtube = new YoutubeService(new BaseClientService.Initializer() {
    ApiKey = credentials.ApiKey
});

SearchResource.ListRequest listRequest = youtube.Search.List("snippet");
listRequest.Q = CommandLine.RequestUserInput<string>("Search term: ");
listRequest.Order = SearchResource.Order.Relevance;

SearchListResponse searchResponse = listRequest.Fetch();
foreach (SearchResult searchResult in searchResponse.Items)
{
   //USE OS RESULTADOS COMO DESEJAR
}

Full example: https://developers.google.com/youtube/v3/code_samples/dotnet

Download lib: https://developers.google.com/api-client-library/dotnet/apis/youtube/v3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.