YouTube Data API Recent Uploads

GeekThis remains completely ad-free with zero trackers for your convenience and privacy. If you would like to support the site, please consider giving a small contribution at Buy Me a Coffee.

I spend far too much time on YouTube, but not for the reasons you may think. I love working with the YouTube API and modifying YouTube to make it better (unofficially). Because of my love for this, I am going to explain how you can get started on using the YouTube Data API Version 3. This tutorial will outline setting up your Developer Keys and performing some basic requests such as getting recent Channel Uploads, and then finally parsing the data returned.

What you Need to Know

YouTube’s Data API now requires all requests to have authorization. This can either be a single developer key or an oAuth key and signature sent to the server. YouTube’s old API (v2) didn’t require any key for requests, but since the surge in users, having a developer key is required to limit requests and monitor them.

Getting your hands on a developer key is simple and free. You need to navigate over to cloud.google.com/console and setup a Project and Application. When I setup my first project, Google required me to verify my phone number. If you don’t have a phone number, a way I found around it is to go back to the old Console, enable the API you want, and then switch back to the new Console.

  1. Navigate to the Google Cloud Console at cloud.google.com/console. If given the option, switch to the new Console.
  2. Create a new Project. The project Name and Project ID don’t matter all that much, but you should note that a Project ID cannot be changed.
  3. When inside of your Project, on the sidebar go to APIs and enable the APIs you want. For this tutorial, you will need to turn on YouTube Data API v3. I suggest looking at the others also to get future ideas.
  4. Navigate to “Registered Apps” and create a new one, or use an existing one. Each app will have it’s own API Keys and signatures to help keep track of them.

Of the available authorization methods for your App, I mostly work with Server Key and OAuth Client ID’s. Server Keys are great if you wish to grab public video information and perform searches and normal statistic queries. OAuth is required if you want the users information such as their unlisted and private videos, changing video information, and altering their account.

Now that your App is all set up, we can move onto making our first request using either the Server Keys or Browser Key. These keys allow us to use YouTube as a user that isn’t logged in. So you can still perform searches and get channel videos, but not make any changes or view private and unlisted videos.

Getting Recent Channel Uploads

At this point, the method in which you use to get the page content isn’t important. Later on you will use the url we are creating inside your program or script to automate it. But for right now, we are just going to enter the url into our browser to test the output data.

We start with the base url of https://www.googleapis.com/youtube/v3/search. The search API is used to get lists of videos.

We then want to add the following parameters to the URL so we can get the content we so desire. We will need a key, part, order, and channelId parameter. The key should be your server or web browser API Key (keep this a secret). Part should be the value “snippet”. Order should be the order you want the videos to appear. The available ordering methods are “date”, “rating”, “relevance”, “title”, and “viewCount”. Last the channelId should be the channel’s ID. If you are unsure where you can find this, you can either build another API Request or on YouTube, go to the “About” tab for that user and click on “Send Message”. In the URL, there will be a parameter of “to_users_ext_ids”, and this is the channel ID, along with the letters “UC” in the front of the ID you copied from the URL. You can verify this by navigating to YouTube.com/channel/channel-id.

Using the above parameters and our URL, we get the new URL similar to the one below.

https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=CHANNEL_ID&order=date&key=KEY

Visiting this page will give us a JSON page where we can easily parse it. Almost every language has a library to parse JSON, including PHP, JavaScript, C and others.

{
	"kind": "youtube#searchListResponse",
	"etag": "\"GbgM9_0DKhSLzW6BxAmRI/fnf7t-smVHh31vUe_2s\"",
	"nextPageToken": "CAUQAA",
	"pageInfo": {
		"totalResults": 81,
		"resultsPerPage": 5
	},
	"items": [
		{
			"kind": "youtube#searchResult",
			"etag": "\"GbgM9_0DKhSLzW6BZH9RI/a3ynTlaLa5hh1BqA\"",
			"id": {
				"kind": "youtube#video",
				"videoId": "3DSmo_dLAmM"
			},
			"snippet": {
				"publishedAt": "2012-11-21T22:23:54.000Z",
				"channelId": "UCjdbOdrjTVonDK9CwZKlhAg",
				"title": "SketchUp: Drawing Terrain and Landscapes",
				"description": "David is back with another tutorial for SketchUp 8, this time using an extension to help draw terrain. You will no longer be limited to drawing on flat land....",
				"thumbnails": {
					"default": {
						"url": "https://i.ytimg.com/vi/3DSmo_dLAmM/default.jpg"
					},
					"medium": {
						"url": "https://i.ytimg.com/vi/3DSmo_dLAmM/mqdefault.jpg"
					},
					"high": {
						"url": "https://i.ytimg.com/vi/3DSmo_dLAmM/hqdefault.jpg"
					}
				},
				"channelTitle": "geekthisnet",
				"liveBroadcastContent": "none"
			}
		}
	]
}

Now you will just have to code to loop through the items array to get each video of the first 5. You can change the limit by adding the parameter maxResults. If you need more information about each video, you will have to perform another request to get a single video’s information.

For even more parameter options, check out the YouTube Developers page for Search Parameters at developers.google.com/youtube/v3/docs/search/list and also check out the main YouTube Developers site for even more query options.

Related Posts

Twitter API 1.1 Get Tweets

How to use Twitter API 1.1 to get recent tweets to display on your website.