You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

YouTube.Subscriptions.list接口无法返回全部订阅问题排查

问题:YouTube API仅返回9条订阅,实际订阅数为488

我订阅了约500个YouTube频道,想要实现批量取消订阅的自动化操作。第一步获取订阅列表时,使用subscriptions.list方法仅能检索到9条结果,但实际订阅数为488:
显示订阅数为488的截图

我已在运行代码的AppsScript以及Google API Explorer中进行尝试,结果一致。

API请求

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

API响应

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

AppsScript函数

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
}

执行结果

4:19:29 PM  Notice  Execution started
4:19:29 PM  Info    Total subscriptions retrieved: 9
4:19:29 PM  Notice  Execution completed

可能的原因及解决方法

1. 授权账号不匹配

确认API Explorer或AppsScript中授权的Google账号,和你拥有488个订阅的YouTube账号是同一个。很多时候会误选其他关联账号,导致返回该账号的订阅数据。

2. 权限范围不足

调用subscriptions.list接口必须请求正确的OAuth 2.0权限:

  • 仅获取订阅:使用https://www.googleapis.com/auth/youtube.readonly
  • 需要取消订阅:使用https://www.googleapis.com/auth/youtube或https://www.googleapis.com/auth/youtube.force-ssl
    如果授权时未申请这些权限,API只会返回少量公开订阅。在AppsScript中,需在appsscript.json配置正确的权限:
{
  "oauthScopes": [
    "https://www.googleapis.com/auth/youtube.readonly",
    "https://www.googleapis.com/auth/script.external_request"
  ]
}

3. 请求参数遗漏或错误

检查请求中是否额外添加了过滤参数(如channelId),这类参数会限制返回结果。同时可以尝试添加order=alphabetical参数,确保分页逻辑正常触发,避免因默认排序导致的结果截断。

4. 失效订阅过滤

部分订阅的频道可能已被删除、终止或因合规限制无法被API返回,但这种情况不会导致结果差距如此之大,优先级低于前三个原因。


内容的提问来源于stack exchange,提问作者tangybuttz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 14:19:53