user1569339
user1569339

Reputation: 683

Thumbnail height & width not being returned

When performing a search request to the API, the height & width fields of the thumbnails are not included in the response, even when specified in the fields parameter. Here's an example for a JS object that provides the API parameters:

{
    part: 'snippet',
    type: 'video',
    order: 'relevance',
    q: 'test',
    fields: 'items(snippet(thumbnails(high(url,height,width))))'
}

Which translates into the following request URL:

https://www.googleapis.com/youtube/v3/search?order=relevance&part=snippet&q=test&fields=items(snippet(thumbnails(high(url%2Cheight%2Cwidth))))&type=video&key={YOUR_API_KEY}

This call yields the following response without the width or height of the thumbnails.

{
 "items": [
  {
   "snippet": {
    "thumbnails": {
     "high": {
      "url": "https://i.ytimg.com/vi/3HKs8WTGzw8/hqdefault.jpg"
     }
    }
   }
  },
  {
   "snippet": {
    "thumbnails": {
     "high": {
      "url": "https://i.ytimg.com/vi/vW_8K_mLtsU/hqdefault.jpg"
     }
    }
   }
  },
  {
   "snippet": {
    "thumbnails": {
     "high": {
      "url": "https://i.ytimg.com/vi/4Yk-jd4BHys/hqdefault.jpg"
     }
    }
   }
  },
  {
   "snippet": {
    "thumbnails": {
     "high": {
      "url": "https://i.ytimg.com/vi/HU9mnag7vSM/hqdefault.jpg"
     }
    }
   }
  },
  {
   "snippet": {
    "thumbnails": {
     "high": {
      "url": "https://i.ytimg.com/vi/pyrH7b0zHwU/hqdefault.jpg"
     }
    }
   }
  }
 ]
}

This similarly does not work for the default or medium thumbnail keys either.

How can these fields be retrieved?

Upvotes: 1

Views: 265

Answers (2)

Ashish Chaturvedi
Ashish Chaturvedi

Reputation: 1379

As per Youtube Date API (v3) Search Method returns as per document but its not working properly.

Now you should try alternative method by the API call of Video.

URL :- https://www.googleapis.com/youtube/v3/videos?part=snippet&id={VIDEO_ID}&key={YOUR_API_KEY}

VIDEO_ID = Return by Search API

YOUR_API_KEY = Google Project API key

Try it

$.get(
            "https://www.googleapis.com/youtube/v3/search",{
            order:'relevance',
            part : 'snippet', 
            type : 'video',
            q: 'test',
            key: 'XXXXXXX'},
            function(data) {
               alert(data.items.length);
               $.each( data.items, function( i, item ) {
                   pid = item.id.videoId;
                   getVids(pid);
               });
           }
         );

         //Get Videos
         function getVids(pid){
            $.get(
                 "https://www.googleapis.com/youtube/v3/videos",{
                 part : 'snippet', 
                 id : pid,
                 key: 'XXXXXXXX'},
                 function(data) {
                     //Code whatever you want 
                 }
             );
         }

Upvotes: 0

johnh10
johnh10

Reputation: 4185

The search endpoint won't return those details. You'll have to take the IDs returned from the search and do another API call to the videos endpoint for the snippet. For instance

https://www.googleapis.com/youtube/v3/videos?part=snippet&id={VIDEO_ID}&key={YOUR_API_KEY}

Upvotes: 3

Related Questions