YouTube API Key Setup: Export and Manage Video Data in Google Sheets

To retrieve video data and channel information from YouTube and manage it in Google Sheets, the YouTube API is a powerful and convenient tool.

By using the API, you can automatically import information from YouTube.

This article will show you how to obtain the YouTube Data API key, which is the most typical of the several YouTube APIs.

This will be a preparation for how to use the API to compile video data into a spreadsheet.

TOC

What is the YouTube API?

The YouTube API is a mechanism that allows external applications and websites to access YouTube videos and channel information.

This allows data to be managed and displayed in spreadsheets and other formats.

There are several types of YouTube APIs, but the most common is the YouTube Data API.

You will have access to the following information

  • Obtain video title, number of views, number of comments, etc.
  • List of subscribers and uploaded videos on the channel
  • Creating and editing playlists

Steps to Obtain a YouTube Data API Key

Obtaining a YouTube Data API Key

STEP
Creating a Google Cloud Platform Account

Go to the official Google Cloud Platform (GCP) website.

If you are using it for the first time, click “Get Started for Free”, and log in with your Google account or create a new account.

STEP
Selecting a Project

Once you’re on the dashboard, click “Select a project” located at the top left of the screen.

Here, create a new project and proceed with preparing to use the API.

STEP
Creating a Project

Click “New Project” to create a new project.

STEP
Set a Name for Your Project.

Decide on a project name and click the “Create” button.

The project name is pre-filled by default, but you can change it to any name you like.

STEP
Select the Project You Just Created

From “Select a project”, choose the project you just created.

STEP
Select “APIs & Services”.

After selecting your project, click on “APIs & Services”.

STEP
Select “Library”

Next, navigate to the “Library”.

STEP
Search for YouTube Data API

Search for “YouTube Data API v3”.

STEP
Enable the API

From the search results, select “YouTube Data API v3” and click the “Enable” button to activate the API.

STEP
Obtaining an API Key

Go to “APIs & Services” > “Credentials”, then click “Create Credentials” and select “API Key”.

STEP
Copy the Generated API Key.

Copy the generated API key and save it for later use in Google Apps Script.

If you are only accessing public data, the OAuth consent screen is not required, and you can use the API key alone with the YouTube Data API v3.

Saving the API Key in Script Properties

To use the API key in Google Apps Script, save it in the Script Properties.

STEP
Open the Apps Script Editor.

Open your Google Sheets, go to the “Extensions” menu, and click on “Apps Script” to open the editor.

STEP
Open the Project Settings

In the Apps Script editor, select “Project Settings”.

STEP
Add Script Properties

Select “Add Script Properties”.

Enter a key name (e.g., “YOUTUBE_DATA_API_KEY”) and the API key.

Once entered, click “Save Script Properties”.

Testing the API Request

Once the API key has been obtained and saved in the Script Properties, execute a simple request to confirm that the API is working correctly.

STEP
Creating the Script

function myFunction(){

}

is included from the beginning, delete it and paste the following script.

function testYouTubeAPI() {
  var apiKey = PropertiesService.getScriptProperties().getProperty('YOUTUBE_DATA_API_KEY');
  var channelId = 'YOUR_CHANNEL_ID'; // Enter the ID of the channel you want to retrieve data from
  var url = `https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&id=${channelId}&key=${apiKey}`;
  
  var response = UrlFetchApp.fetch(url);
  var data = JSON.parse(response.getContentText());

  Logger.log(data); // Output channel information to the log
}
  • The apiKey contains the API key retrieved from the Script Properties.
  • Specify the ID of the channel you want to retrieve in channelId.
  • Construct the API request URL and retrieve the data using UrlFetchApp.fetch.
  • The retrieved data is in JSON format, so you can parse it with JSON.parse and check the channel information using Logger.log.
STEP
Save the Script

After writing the script, name and save it.

(e.g., “Youtube Data Acquisition Test”)


(Don’t forget to replace the YOUR_CHANNEL_ID part in the script)

STEP
Run Script

When the execution completes without any error messages and data is output to the log, the YouTube Data API is ready.

If you’re running the script for the first time, you need to authorize it.

Therefore, press “Review Permissions.

Detailed Authorization Steps

Press “Advanced.”

Press “Go to Untitled project (Unsafe).”

After that, press “Allow.”

Conclusion

In this article, I introduced how to obtain and set up the YouTube Data API.

You are now ready to access YouTube data using your API key.

I will show you how to compile video information into a spreadsheet in another article.

Our company offers support for improving work efficiency through the use of Google Apps Script.
If you need assistance with Google Apps Script customization or error resolution, please feel free to contact us.
We are fully committed to supporting your business improvements.

Contact us here

Let's share this post !

Comments

To comment

TOC