↑ Return to F90 Youtube

F93 Youtube API

George Morgan
My articles
Follow on:

Page no: F93




Source: Google Developers


For fetching youtube videas, we are using Youtube API provided by Google Developers.




Obtaining authorization credentials

  1. Open the Credentials page in the API Console.
  2. This API supports two types of credentials. Create whichever credentials are appropriate for your project:
    • OAuth 2.0: Whenever your application requests private user data, it must send an OAuth 2.0 token along with the request. Your application first sends a client ID and, possibly, a client secret to obtain a token. You can generate OAuth 2.0 credentials for web applications, service accounts, or installed applications.For more information, see the OAuth 2.0 documentation.
    • API keys: A request that does not provide an OAuth 2.0 token must send an API key. The key identifies your project and provides API access, quota, and reports.The API supports several types of restrictions on API keys. If the API key that you need doesn’t already exist, then create an API key in the Console by clicking Create credentials > API key. You can restrict the key before using it in production by clicking Restrict key and selecting one of the Restrictions.
  3. Use these credentials to connect to Youtube API and fetch whenever you want.

API Methods

Video methods


The API supports the following methods for videos resources:

Retrieves the ratings that the authorized user gave to a list of specified videos.
Returns a list of videos that match the API request parameters.
Uploads a video to YouTube and optionally sets the video’s metadata.
Updates a video’s metadata.
Deletes a YouTube video.
Add a like or dislike rating to a video or remove a rating from a video.
Report a video for containing abusive content.

Video attributes

Full list can be found here. 



Other methods of Youtube API

  • Comments
  • Channels
  • Captions
  • Reviews
  • Videos
  • Video Categories
  • Thumbnails
  • Subscriptions
  • Search
  • Playlists & Playlist items



if(isset($_GET[‘data’])) {
$searchQuery = ”;
if (isset($_GET[‘data’][‘youtube_search_channelId’]) && !empty($_GET[‘data’][‘youtube_search_channelId’])) {
$searchQuery .= ‘&channelId=’.$_GET[‘data’][‘youtube_search_channelId’];
if (isset($_GET[‘data’][‘youtube_search_q’]) && !empty($_GET[‘data’][‘youtube_search_q’])) {
$search_q = ‘&q=’.$_GET[‘data’][‘youtube_search_q’];
$searchQuery .= ‘&q=’.$_GET[‘data’][‘youtube_search_q’];
if (isset($_GET[‘data’][‘youtube_search_type’]) && !empty($_GET[‘data’][‘youtube_search_type’])) {
$searchQuery .= ‘&type=’.$_GET[‘data’][‘youtube_search_type’];
if (isset($_GET[‘data’][‘youtube_search_tags’]) && !empty($_GET[‘data’][‘youtube_search_tags’])) {
if (!@$search_q) {
$searchQuery .= ‘&q=’.$_GET[‘data’][‘youtube_search_tags’];
} else {
$searchQuery .= ‘ | ‘ . $_GET[‘data’][‘youtube_search_tags’];

$youtube_api_key = get_option(‘youtube_api_key’, false);
$youtube_api_key_videos = get_option(‘youtube_api_key_videos’, false);
if (!$youtube_api_key) {
echo “<br><h2>Please, first set API Key in settings page!</h2>”;
echo ‘<br><div class=”alert alert-success fade in alert-dismissible”>Xml Genereted. View in browser from <a href=”/wp-content/plugins/youtube_video_loader/youtube_load_data.php?key=’.$youtube_api_key.$searchQuery.'” target=”_blank”>HERE!</a></div>’;

if (isset($_GET[‘show_result’])) {
$show_results = true;
} else {
$show_results = false;


API for Comments

  • — How does Google index (its own) videos on website
  • using the own index value in Youtube…. probably
  • Implemented everywhere
  • —- Bug: Many videos loaded without comments
  • many videos have many comments, but we load them without
  • —- Solution Proposal
  • if video is loaded too quickly –> reload comments


Currently we use functions from these methods:

  • Chanels
  • Videos
  • Video Categories
  • Thumbnails
  • Search
  • Playlists



if (isset($_GET[‘key’]) && !empty($_GET[‘key’]) && !isset($_GET[‘ids’])) {
$youtube_api_key = $_GET[‘key’];

$searchQuery = ”;
if (isset($_GET[‘channelId’]) && !empty($_GET[‘channelId’])) {
$searchQuery .= ‘&channelId=’.$_GET[‘channelId’];
if (isset($_GET[‘q’]) && !empty($_GET[‘q’])) {
$searchQuery .= ‘&q=’.$_GET[‘q’];
if (isset($_GET[‘type’]) && !empty($_GET[‘type’])) {
$searchQuery .= ‘&type=’.$_GET[‘type’];

// $result = file_get_contents(‘https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&maxResults=10′.$searchQuery.’&key=’.$youtube_api_key);
$result = file_get_contents(‘https://www.googleapis.com/youtube/v3/search?order=date&maxResults=10&part=snippet’.$searchQuery.’&key=’.$youtube_api_key);

if (!empty($result)) {
$result = (array) json_decode($result);

if (is_array($result)) {
echo $xml_header;
foreach ($result[‘items’] as $key => $item) {
if (isset($_GET[‘debug’])) {
echo “<pre>”;




  1. Connect to youtube API
  2. Try to search for an author’s video
  3. store in DB into the new post type “videos”
  4. Make a GUI for youtube API nearly the same as Books GUI + debugger
  5. Add a new field youtube channel, so we can syndicate all videos from youtube


We enhanced our script to a small wordpress plugin nearly the same as Amazon book loader.

In this plugin you can search for a video or channel and we automatically add the video.


Youtube quota


There is a calculator where you can calculate every single query how many quota it takes. 


You have quota of 10 thousand units per day.



Per video – Load a video costs 1 unit.  (currently on one API)

Per search – Execute a search costs 100 units. (currently on one API)


We hit quota due to too many searches during the tests.


We introduce 3 new fields for keys. We also divided keys to search and videos keys.

Youtube category mapping


Youtube category id Youtube category name WP Category Id  Wp Category Name
1 Film & Animation
2 Autos & Vehicles
10 Music
15 Pets & Animals
17 Sports
18 Short Movies
19 Travel & Events
20 Gaming
21 Videoblogging
22 People & Blogs
23 Comedy
24 Entertainment
25 News & Politics
26 Howto & Style
27 Education
28 Science & Technology
29 Nonprofits & Activism
30 Movies
31 Anime/Animation
32 Action/Adventure
33 Classics
34 Comedy
35 Documentary
36 Drama
37 Family
38 Foreign
39 Horror
40 Sci-Fi/Fantasy
41 Thriller
42 Shorts
43 Shows
44 Trailersa


See more for Autofeed