2

So I've been playing around with Youtube API V3 lately. I tried to retreive all the subscriptions I have in my account and for that I used the code sample they had on their site, which is the following:

# -*- coding: utf-8 -*-

# Sample Python code for youtube.subscriptions.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/code-samples#python

import os

import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.subscriptions().list(
        part="snippet,contentDetails",
        mine=True
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

Although this works fine, It only retreives information of 5-6 channels, while I am subscribed to over 200 channells, I made sure I didn't exceed my quota either. Any ideas why?

  • 1
    Maybe there's some kind of filtering going on, or pagination, or it only shows channels of certain types (make sure to check if there's any commonalities between them), or there's some other issue. Without seeing your output or seeing what the specific channels are, I'm not sure there's much we can do to help unless an expert steps in. Did you try this on other channels? Could you get more of their subscriptions? – Random Davis Sep 20 '22 at 21:46
  • I think the OP has the same problem as I had - not using pagination. I'll leave [my own answer](https://stackoverflow.com/a/73410215/12511801) and I hope OP gets the idea. – Marco Aurelio Fernandez Reyes Sep 20 '22 at 23:00
  • 1
    @RandomDavis The problem turned out to be what Marco said, I was not implementing the pagination, so each time the script ran, It only fetched the channels in the first page. Using the code snippet from Marco's thread (nextPageToken precisely) made my script work. Thanks lads. – ChrysippusDiedLaughing Sep 21 '22 at 13:23
  • 1
    @MarcoAurelioFernandezReyes Yup, Using your method made my script work fine, I was just not using pagination. – ChrysippusDiedLaughing Sep 21 '22 at 13:24

1 Answers1

1

As commenters said, you need to implement pagination. To receive more results per request, you can also change maxResults to 50 instead of the default 5 value (that's why you are talking about receiving only 5-6 results).

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33