Facebook Pixel

The guide to automatically import your YouTube content on your website using the YouTube API key

The guide to automatically import your YouTube content on your website using the YouTube API key
Do you have a YouTube channel with several videos and dream of hosting it directly on your website? If so, this article is for you. Indeed, Google via its YouTube API key gives you the possibility to host your channel on your website. Therefore, this guide tells you how to use the YouTube API V3 key and channel ID to sync content. Our guide consists of three parts. In the first, you will see how to create the required credentials. Then in the second part, we will provide you with codes that you will use to do the synchronization. Finally, in the third, we will give you tips to simplify the procedure depending on the type of web solution you use. In particular, if you are on WordPress, extensions allow you to save time.

Why host your YouTube channel on your website?

Hosting your YouTube channel on your site has several advantages:
  • Expand your reach: Generally speaking, visitors need to go to YouTube to watch YouTube videos. Moreover, YouTube is full of millions of videos that will potentially obstruct the accessibility of yours. Integrating your YouTube channel on your website is, therefore, a way to target people outside of YouTube, but also to promote specific access to your content.
  • Increase the visibility of your website. Video content seems to have more reach than articles. To this end, having your videos on your website will certainly help increase your visibility.
  • Grow your brand. The more popular you are online, the more likely you are to grow your brand.

The different ways to embed YouTube videos on your website

There are two ways to embed YouTube videos on your site:
  • The manual way: you can use an iFrame or a JavaScript library in this case. We've written a detailed guide if you're interested in this method. However, manual embedding usually allows you to embed videos one by one, which is a pain.
  • Use of the YouTube API key: here you can connect your website to your YouTube channel and automatically see your videos appear on your website as soon as you add them. In other words, you update your video library and the codes take care of updating your website. This method offers you more advantages.

Some Benefits of YouTube API Key

APIs (or Application Programming Interfaces) give you the ability to connect to data from an external platform as easily as possible. With the YouTube API, you can connect shares from your YouTube channel to your website. Below are some benefits compared to the manual procedure:
  • No need to copy the link of your video manually: thanks to the API, the videos of your channel are automatically updated on your site. You no longer need to manually manipulate links to ensure the integration of a video on your website.
  • The possibility of creating a video library: it's not just a few videos that you can integrate thanks to the API, but your entire channel. So, with the API, you will potentially create a video library and keep your visitors on your website.
  • Flexibility in data manipulation: with the API you can, for example, get the description, summary, and banner photo of your video. In addition, you can customize the design of your site to your liking.

Prerequisites for connecting YouTube to your website

To be able to connect your channel to your site, you will need:
  • from a YouTube channel,
  • of a website and
  • from an account to Google Cloud.
However, if you are missing one or more of these tools, have no fear. In the following, we explain how to obtain them.

How to Create a YouTube Channel

All Gmail users automatically get a YouTube channel. So to create your YouTube channel, you will first need to have a Gmail or G-Suite account. However, if you don't have it, the procedure for creating your channel includes a sub-procedure for creating your Gmail account.

For all Gmail and G-Suite subscribers

  • Go to the YouTube site and click "connect". When you click on the button, the site will redirect you to the login page.
  • Enter your username and password to log in.
  • You are then redirected to the YouTube page. Click the camera icon in the top-right corner to add your videos.

If you don't have a Gmail or G Suite account

  • Go to YouTube and click on “connect”. Once done, you will see a page asking you to create your account.
  • Click on “Create an account” (you have the choice between creating an account for yourself and managing your business).
  • Choose the option that fits your goals.
  • Then, follow the steps suggested by Google to create your account. In most cases, you need your name, your first name, a unique email (which you will select) and the password.
  • To add videos, click the camera icon in the top-right corner of your YouTube page.
youtube api key

How to Create a Google Cloud Account to Retrieve YouTube API Key

The Google Cloud account is an interface proposing a set of APIs, including the YouTube API. To create a Google Cloud account, follow these steps:
  • Go to the Google Cloud site and click on “Go to the console”.
  • Like creating the YouTube account, you will be redirected to the login page.
  • On this page, you must use an existing Gmail or G-Suite account or create one.
  • Once connected, follow the steps recommended by the platform to configure your account.
  • You must add payment information to be able to activate your account
Kindly note that Google Cloud offers paid applications based on the number of daily requests. Admittedly, you might never have to pay if you use a few apps. However, we advise you to refer to the general conditions before committing yourself. youtube api key

How to create a website?

If you want to connect YouTube to your site, you definitely have one. Otherwise, the easiest option to use this article is to have a self-hosted site. Indeed, to be able to modify the codes of your site, you must have the necessary authorization. This freedom is not offered by online website builders or free solutions.

Option 1: Create your website

The generic steps for creating a self-managed site are as follows:
  • Find a domain name and a host: several hosts give you the possibility of having a domain name for free. So, you will find, for example, options for less than $3.95 per month online.
  • Choose the solution that interests you to create your website. We recommend WordPress.
  • Create your site.

Option 2: Hire a developer

A developer or a web agency can help you create your website. This option is best if you want a quality solution. Our agency offers you, for example, solutions adapted to your needs at a competitive cost. In case you are keen to know more about what we offer, visit our services or contact us.

How to Create a YouTube API Key and Retrieve YouTube Channel ID

If you have all the elements described above, namely, a website, a YouTube channel, and a Google Cloud account, then you are ready for the next step.

Create YouTube API Key

You must have the API key to sync your website with YouTube. To obtain this API key, you must follow these steps:
  • First, visit the Google API Library
youtube api key
  • Then, in the search field, enter YouTube Data API v3
  • Finally, click on the activate button.
When you click on activate, the site generates a code (API) that you will use later. Therefore, we recommend that you note it.

Retrieve YouTube Channel ID

To retrieve your YouTube channel ID, you must:
  • Log in to your YouTube account where your channel is located.
  • Then, in the top-right corner of the page, click on "setting/Configuration"
  • In the left menu, click on "Advanced Settings/Advanced configuration"
  • You will then find your identifiers among the list of other information on the page
Your YouTube channel ID is also located on the URL when you follow these steps:
  • Click on your profile picture (top-right corner) and a drop-down menu will appear.
  • Then click on “Your Channel”.
  • You will see your channel ID right after youtube.com/channel/ at the URL.

Use the code below to display your videos on your site


<?php
$API_key    = 'xxxx'; // Remplacez par votre clé API
$channelID  = 'xxx';  // Remplacez par votre identifiant Youtube
$maxResults = 10;

// Faire un Appel API pour récuperer la liste des vidéos en format Json
$myQuery = "https://www.googleapis.com/youtube/v3/search?key=$API_key&channelId=$channelID&part=snippet,id&order=date&maxResults=$maxResults";
$ch = curl_init();
    
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $myQuery);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
        
    curl_close($ch);
        
    $data = json_decode($response);
        
   $decoded= json_decode(json_encode($data), true);
 
 
// Run a loop to display list of videos
foreach ($decoded['items'] as $items)
{
$id = $items['id']['videoId'];
$title= $items['snippet']['title'];
$description = $items['snippet']['description'];
$thumbnail = $items['snippet']['thumbnails']['default']['url'];
if(!empty($id)) {
?>
<div class="col-md-4">
    <div class="videoWrap controllles">
                            <img src="<?php echo $thumbnail; ?>" alt="<?php echo $post->post_title; ?>">
                            <div class="play_video">
                                <a class="video_popup" href="<?php echo "https://www.youtube.com/watch?v=".$id; ?>"><i class="fa fa-play"></i></a>
                            </div>
                        </div>
                        <div class="videoblog">
                        <h2><?php echo $title; ?></h2>
                        <p><?php echo $description; ?></p>
                        </div>
</div>
<?php
}
}
?>
Lines 2 and 3 of the code allow you to add your API key and then your YouTube ID, respectively. Next, line 7 (myQuery) retrieves the YouTube account information using the key and ID. All the following are just codes to browse the data to bring up the video list.

Add some CSS to format your page

Add JavaScript to make your page interactive or keep the video playing on your website

Note that the JavaScript code uses jQuery. You must include it in your code if you want to use it. The result of this program will be more or less similar to the screenshot below. On-page hosting display

An alternative for WordPress users

WordPress is a content management system that provides an easy way to create and manage websites. Indeed, on WordPress, it is possible to use just one extension to solve a problem. Thus, we have developed a WordPress extension that greatly simplifies the steps of this tutorial. So to host your YouTube channel on your website, all you have to do is install and configure our extension. You will find the latter in our plugin bank.

How does it work?

After installing the extension, you must:
  • Visit Settings tab ⇾ Vlog Generator option
  • Enter your API credentials and your YouTube channel key
  • Specify the number of data you want to extract
  • Mention the number of data per page.
Once you save the changes, you can use the shortcode below to make the videos appear on your website.
[psw_youtubevideo]
vlog form

Conclusion

Easily managing your YouTube channel on your website can be relatively simple. If you are on WordPress, you will be happy to use our extension. However, if you are not, we hope that our guide will help you effectively in your maneuver. If you have a connection project with APIs to facilitate your daily activities, contact us.