vimal mishra
vimal mishra

Reputation: 1167

How to get most viewed you tube video using javascript API?

I am trying to get the list of video sorted by most viewed you tube video.I have been trying the following code but it is not returning any response.

var request = gapi.client.youtube.search.list({
                part: 'snippet,contentDetails,statistics',
                q: query,
                maxResults: count,
                order:'viewCount'
            });
request.execute(onSearchResponse);

Above code does not return any response.Can someone please redirect me in right direction I have already looked into documentation.

https://developers.google.com/youtube/v3/docs/search/list

Upvotes: 0

Views: 660

Answers (1)

Mauricio Arias Olave
Mauricio Arias Olave

Reputation: 2575

Using the YouTube API v3 (and modifying the parameters supplied in this answer), you can use this request URL for get the most viewed YouTube video.

https://www.googleapis.com/youtube/v3/search?key=<YOUR_API_KEY>&part=snippet&order=viewcount&maxResults=1

The following information was taken from the YouTube Data API - official documentation:

  • key: your API key.
  • part: The part parameter specifies a comma-separated list of one or more search resource properties that the API response will include. Set the parameter value to snippet.
  • order1: in this case, is used viewcount.
  • maxResults: specifies the maximum number of items that should be returned in the result set. Acceptable values are 0 to 50, inclusive. The default value is 5.

1 Linked documentation says that only these values are allowed in the order parameter:

  • date: Resources are sorted in reverse chronological order based on the date they were created.
  • rating: Resources are sorted from highest to lowest rating.
  • relevance: Resources are sorted based on their relevance to the search query. This is the default value for this parameter.
  • title: Resources are sorted alphabetically by title.
  • videoCount: Channels are sorted in descending order of their number of uploaded videos.
  • viewCount: Resources are sorted from highest to lowest number of views. For live broadcasts, videos are sorted by number of concurrent viewers while the broadcasts are ongoing.

Upvotes: 1

Related Questions