The following examples show how to use the YouTube Data API (v3) to perform functions related to subscriptions.
Retrieve a channel's subscriptions
Call the
subscriptions.list
method to retrieve subscriptions for a particular channel. There are two ways to
identify the channel:
-
To retrieve the authenticated user's subscriptions, set the
mineparameter's value totrue. A request that uses themineparameter must be authorized using OAuth 2.0.https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.list? part=snippet,contentDetails &mine=true
-
To retrieve subscriptions for any other channel, set the
channelIdparameter's value to that channel's unique YouTube channel ID. The following example retrieves a list of channels subscribed to by the TED channel on YouTube.https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.list? part=snippet,contentDetails &channelId=UCAuUUnT6oDeKwE6v1NGQxug
Note: The API returns a
403 (Forbidden)HTTP response code if the specified channel does not publicly expose its subscriptions and the request is not authorized by the channel's owner.
See the
subscriptions.list
method's documentation for code samples.
Add a subscription
Call the
subscriptions.insert
method to add a channel subscription. This request must be authorized using
OAuth 2.0. The request body is a
subscription resource that
sets the following values:
- The
snippet.resourceId.kindcontains the valueyoutube#channel. - The
snippet.resourceId.channelIdproperty identifies the channel that is being subscribed to. The property value is a unique YouTube channel ID. The channel ID could be obtained in multiple ways, including calling thechannels.listmethod or retrieving search results for channels.
The following API request subscribes you to the TED channel on YouTube:
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.insert? part=snippet
The request body is:
{
"snippet": {
"resourceId": {
"kind": "youtube#channel",
"videoId": "UCAuUUnT6oDeKwE6v1NGQxug"
}
}
}
See the
subscriptions.insert
method's documentation for code samples.
Delete a subscription
This example deletes a subscription. This request must be authorized using OAuth 2.0. This example has two steps:
-
Step 1: Retrieve the subscriptions for the authenticated user's channel
Call the
subscriptions.listmethod to retrieve the list of subscriptions. The preceding example for retrieving a channel's subscriptions explains how to make this request.The application calling the API could process the API response to display a list of subscriptions, using each subscription's ID as a key. In the response, each item's
idproperty identifies the subscription ID that uniquely identifies the corresponding subscription. You will use that value to remove an item from the list in the next step. -
Step 2: Delete a subscription
Call the
subscriptions.deletemethod to delete a subscription. Set the request'sidparameter to the subscription ID for the subscription that you want to remove. This request must be authorized using OAuth 2.0.To complete the request in the APIs Explorer, you need to set the
idproperty's value.https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.delete? id=SUBSCRIPTION_ID
See the
subscriptions.delete
method's documentation for code samples.
Retrieve a list of subscribers to the authorized user's channel
To retrieve a list of channels that subscribe to the authenticated user's
channel, call the subscriptions.list method and set the
mySubscribers
parameter's value to true. The request must be authorized using OAuth 2.0.
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.list? part=snippet,contentDetails &mySubscribers=true