In this article, we will be discussing about the Youtube Subscriptions. How API can be used to retrieve and manipulate these subscriptions.
There are three operations that we will be discussing:
- List all the subscriptions associated with a Youtube channel
- Insert a new subscription
- Delete a subscription.
All these methods will be requiring user’s authorization, so we will be first discussing how to get a OAuth Credential.
Follow the steps below to generate a Client Id and a Secret Key.
- Go to Google Google Developers Console and Click on Sign In in the upper rightmost corner of the page. Sign In using the credentials of the valid Google Account. If you don’t have a google account, setup a account first and then use the details to Sign In on the Google Developers Homepage.
- Now navigate to the Developer Dashboard and create a new Project.
- Click on Enable API option.
- In the search field, search for Youtube Data API and select the Youtube Data API option that comes in the drop down list.
- You will be redirected to a screen that says information about the Youtube Data API, along with two options : ENABLE and TRY API.
- Click on ENABLE option to get started with the API.
- In the sidebar under APIs & Services, select Credentials.
- At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.
- In the Credentials tab, select the Create credentials drop-down list, and choose OAuth Client Id. OAuth is generally used where authorization is required like in the case of retrieving liked videos of a user.
- Select the application type Other, enter the name “YouTube Data API Myvideos”, and click the Create button.
- Click OK.
- Click on Download button to the right of the client Id to download the JSON file.
- Save and rename the file as client_secret.json and move it to the working directory.
Install additional libraries using the pip command:
pip install –upgrade google-auth google-auth-oauthlib google-auth-httplib2
Note: Let’s discuss how to find the channel Id to use it as a parameter in the first code – to lists all subscriptions associated with a Youtube Channel Id.
Follow the steps below to find the channel Id:
- Sign in to your Youtube Account.
- In the top left corner, CLick on three lines icon and go to Settings.
- Now in the same menu in the left hand side it will show option of Advance Settings.
- You will see your Channel Id under Account Information.
Code to list subscriptions: Here are three ways to accomplish this task. We will be discussing these in detail.
- List Subscriptions associated with a channel Id
- List subscriptions of one’s own account
- Check whether a desired subscription exist or not
Code to list all subscriptions associated with a Channel Id: This example shows how channel Id can be used as a parameter to retrieve all subscriptions for that particular Youtube Channel.
While executing the code you will be asked for the authorization code. For getting the code you need to follow the link mentioned in the command prompt screen above the line: Enter the Authorization code.
Now follow the link and copy paste the authorization code that you will get by granting the permission.
We have set the maxResults parameter to 3 i.e. why the output shows only top three subscriptions name. But you can see from the totalResulst value which is 38, which means there are in total 38 subscriptions in the associated channel.