Activities
Learn about the activity resource and how to use it to get a response from an API call.
What is an activity
resource?
Users and channels can perform several actions on YouTube, such as commenting on a video, posting a video, adding a video to a playlist, subscribing to a channel, and more. The details regarding these actions are stored in the activity
resource. A single activity
resource defines the action type, the channel that performed the action, and the resource associated with the action. For example, for a channel uploading a video, the activity
resource would include upload
as the type of action, the channel’s name, and the video uploaded.
Supported methods
The API only supports the list
method for the activity
resource. Using the list
method, we can fetch the details of the activities associated with any channel, regardless of the channel owner.
Let’s use the list
method to retrieve data with the help of a few examples.
Example 1: Retrieve activities from a channel
In this example, we’ll retrieve the 25 most recent activities for a channel, such as the Educative Sessions YouTube channel. We’ll request the API to retrieve the snippet
and contentDetails
parts for the activity
resource.
We’ll use the channel ID to retrieve the recent activities of a channel. A default channel ID has already been added in the code below.
Note: Look at the steps in this lesson to find a channel’s ID.
You can also replace the channelId
in the code below. To add a channel ID of your choice, click on the “Edit” button in the following widget, enter the channel ID in the CHANNEL_ID
field and click the “Save” button.
Note: We’ve set the
maxResults
variable to 25 so that it retrieves a maximum of 25 activities. We can change this number to retrieve any number of activities.
Let’s run the following code to get a response with the 25 recent activities of the Educative Sessions YouTube channel, along with the snippet
and channelDetails
parts.
import osimport jsonimport googleapiclient.discoveryapi_service_name = "youtube"api_version = "v3"DEVELOPER_KEY = "{{API_KEY}}"youtube = googleapiclient.discovery.build(api_service_name, api_version, developerKey = DEVELOPER_KEY)request = youtube.activities().list(part="snippet,contentDetails",channelId="{{CHANNEL_ID}}",maxResults=25)response = request.execute()json_object = json.dumps(response, indent = 4)print(json_object)
In the JSON response received, we can see a list of objects named items
. Each object in this list is an activity
resource, which we can verify by looking at the value of the kind
key, which is youtube#activity
. We’ll also find the nested objects for the snippet
and contentDetails
parts for each activity
resource.
Example 2: Retrieve activities of an authenticated user
This example will retrieve the 25 most recent activities performed by 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.
Since this is an authorized API request, we aren’t restricted to just the channel activity. We can also retrieve information about other activities performed by the authenticated user. We already 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 require the
API_KEY
when making 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 that 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. Additionally, you must have a channel created against your email address.
We’ll be asked to permit the API request by giving some permissions. Finally, 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}}
In the JSON response received, we can see a list of objects named items
. Each object in this list is an activity
resource. In the snippet
object, we’ll see a key type
which identifies the type of activity that the resource describes.