Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

YouTube.Subscriptions.list not returning all subscriptions

$
0
0

I'm subscribed to ~500 YouTube channels and I'm trying to automate unsubscribing from them in bulk.

The first step is listing the channels I'm subscribed to, but I'm only retrieving 9 results using the subscriptions.list method.

Here's the screenshot showing my subscriptions at 488

What am I doing wrong?

I've tried this both in AppsScript (where I'm running the code) and also using the Google API Explorer and the results are the same.

Request:

curl \'https://youtube.googleapis.com/youtube/v3/subscriptions?part=snippet%2CcontentDetails&maxResults=50&mine=true&key=[YOUR_API_KEY]' \  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \  --header 'Accept: application/json' \  --compressed

Response:

{"kind": "youtube#SubscriptionListResponse","etag": "[REMOVED]","pageInfo": {"totalResults": 9,"resultsPerPage": 50  },"items": [    {...

AppsScript Function:

function getAllSubscriptions() {  let subscriptions = [];  let nextPageToken = '';  let totalCount = 0;  // Initialize a counter for total subscriptions  // Continue fetching while there's a nextPageToken (for pagination)  do {    try {      // Call the YouTube API to get a list of subscriptions      const response = YouTube.Subscriptions.list('snippet', {        mine: true,        maxResults: 50,        pageToken: nextPageToken      });      // Ensure the response is valid before accessing properties      if (!response || !response.items) {        logMessage('No response or empty subscription list. Stopping script.');        return subscriptions;  // Return the subscriptions fetched so far      }      // Add each subscription to the array and increment the counter      response.items.forEach(item => {        subscriptions.push({          title: item.snippet.title,       // Channel title          subscriptionId: item.id,         // Subscription ID          channelId: item.snippet.resourceId.channelId // Channel ID (added for exclusion check)        });        totalCount++;  // Increment the total count for each subscription      });      // Update nextPageToken to get the next page of results (if there is one)      nextPageToken = response.nextPageToken;    } catch (e) {      handleError(e);      return subscriptions;  // Stop execution and return the subscriptions fetched so far    }  } while (nextPageToken);  // Log the total number of subscriptions retrieved  logMessage('Total subscriptions retrieved: '+ totalCount);  return subscriptions; // Return the array for further use}

Result:

4:19:29 PM  Notice  Execution started4:19:29 PM  Info    Total subscriptions retrieved: 94:19:29 PM  Notice  Execution completed

Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>