Reputation: 49
I am trying to get list subscribers of any channel using youtube api using python. But i am getting issue when i run my code it asks me to enter authorisation code and i do not know what it is. I have also enabled oath api. see my code below import os
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
import googleapiclient.errors
flow = InstalledAppFlow.from_client_secrets_file("client_secrets.json",
scopes=["https://www.googleapis.com/auth/youtube.readonly"])
flow.run_local_server(port=8080, prompt="consent")
credentials = flow.run_console()
youtube = build("youtube", "v3", credentials=credentials)
request = youtube.subscriptions.list(
part="snippet,contentDetails",
channelId="UC_x5XG1OV2P6uZZ5FSM9Ttw"
)
response = request.execute()
print(response)
is there any way to do so?
Upvotes: 0
Views: 4718
Reputation: 5632
Be aware that every YouTube channel doesn't have its subscriptions public (in fact they are private by default, see youtube.com/account_privacy).
However if they are public, they are available on the:
YouTube UI at youtube.com/channel/CHANNEL_ID/channels?view=56&shelf_id=0
YouTube Data API v3 Subscriptions: list (you don't need to authenticate to retrieve them as they are public). Just retrieve googleapis.com/youtube/v3/subscriptions?part=snippet&channelId=CHANNEL_ID&key=API_KEY.
More precisely this Python code does the job:
import googleapiclient.discovery
youtube = googleapiclient.discovery.build("youtube", "v3", developerKey="API_KEY")
request = youtube.subscriptions().list(
part="snippet,contentDetails",
channelId="CHANNEL_ID"
)
response = request.execute()
print(response)
Example of YouTube channel having public subscriptions: UCt5USYpzzMCYhkirVQGHwKQ
Note: if the channel's subscriptions are private, you'll hit a
Encountered 403 Forbidden with reason "subscriptionForbidden"
// Traceback ...
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://youtube.googleapis.com/youtube/v3/subscriptions?part=snippet%2CcontentDetails&channelId=CHANNEL_ID&key=API_KEY&alt=json returned "The requester is not allowed to access the requested subscriptions.". Details: "[{'message': 'The requester is not allowed to access the requested subscriptions.', 'domain': 'youtube.subscription', 'reason': 'subscriptionForbidden'}]">
subscriptions.list errors documentation
Upvotes: 3