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.
functiontestYouTubeAPI() {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 fromvar 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.
Comments