Channels
Learn about the channel resource and how to use it to get information about a YouTube channel.
What is a channel
resource?
The channel
resource contains information about a YouTube channel, such as the number of subscribers, view count, video count, channel name, and description.
Supported methods
The API supports the list
and update
methods for this resource. The list
method returns the channel resources matching the request criteria using the channel ID or username. It also allows retrieval of channel data of the user authorizing the API request. The update
method updates a channel’s metadata. For this lesson, we’ll take a look at the list
method only.
Let’s use the list
method to retrieve data with the help of a few examples.
Example 1: Retrieve channel data using channel ID
In this example, we’ll retrieve the channel data of the Educative Sessions YouTube channel. It’ll retrieve the snippet
and statistics
parts for the channel
resource.
Note: If you changed the
CHANNEL_ID
parameter in the previous lesson, then replace it with the Educative Sessions channel IDUCT_8FqzTIr2Q1BOtvX_DPPw
in the code below. To do so, click on the “Edit” button in the following widget, enter the channel ID in theCHANNEL_ID
field and click the “Save” button.
Let’s run the following code to get a response with the channel data of the Educative Sessions YouTube channel, along with the snippet
and statistics
parts.
api_service_name = "youtube"api_version = "v3"DEVELOPER_KEY = "{{API_KEY}}"youtube = googleapiclient.discovery.build(api_service_name, api_version, developerKey = DEVELOPER_KEY)request = youtube.channels().list(part="snippet,statistics",id="{{CHANNEL_ID}}")response = request.execute()
In the JSON response received, you’ll see the channel title, description, and other information in the snippet
object. In the statistics
object, we’ll find the channel statistics, such as view count, video count, and so on.
Example 2: Retrieve channel data of an authenticated user
In this example, we’ll retrieve the channel data of the user authorizing the API request.
Note: The
channelId
parameter will not be required for this example. Instead, we’ll use themine
parameter set totrue
. This tells the API call that we’re requesting data from a personal account.
Remember that we created an OAuth 2.0 client ID and saved the JSON object of the client_secret_CLIENTID.json
file. We’ll use that in this example.
Note: We don’t need the
API_KEY
when we make authenticated requests.
After clicking the “Run” button, you’ll see a prompt in the terminal that says “Please visit this URL to authorize this application.” Copy this URL and paste it into a browser tab.
Note: Make sure that you are signed in using the Google account that was used to set up the project in the API Console.
You’ll be asked to permit the API request by giving some permissions. Then, copy the generated code and paste it into the terminal next to the prompt “Enter the authorization code.”
Finally, you’ll see the API response object in the terminal.
{{CLIENT_SECRET}}
For the user authorizing the API request, we get a JSON response with the channel title, description, and other information in the snippet
object. In the statistics
object, we’ll find the channel statistics.