API YouTube V3 - Browse videos by title

3

Hello.

I'm using the YouTube V3 API to fetch videos in a .NET project. It turns out that I have the exact title and need to search for it. In the documentation it is not clear if it is possible to do the title search. API Documentation

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

    
asked by anonymous 21.06.2017 / 15:54

1 answer

0

Considering that you already have the Key API and it is enabled, in summary:

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 the maxResults to 1 to bring the most relevant video based on the name, so if I pass the full name it should be the first one in the list. But if you want to iterate through the list increase the maxResult as in the jsfiddler below.

Test this Fiddler: link

Note: Remember to load JQuery and Google API

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

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: link

Download lib: link

    
22.07.2017 / 15:18