我在使用 Python YouTube API 时遇到错误

I am getting an error while using Python YouTube API

我正在尝试创建一个 Python 程序来获取频道统计信息,但是当我 运行 它时,YouTube API 网站给出了这个输出(错误):

{
  "error": {
    "code": 400,
    "message": "'statisitcs'",
    "errors": [
      {
        "message": "'statisitcs'",
        "domain": "youtube.part",
        "reason": "unknownPart",
        "location": "part",
        "locationType": "parameter"
      }
    ]
  }
}

这是我的代码:

class YTstats:
    def __init__(self, api_key, channel_id):
        self.api_key = api_key
        self.channel_id = channel_id
        self.channel_stats = None

    def get_channel_statistics(self):
        url = f'https://www.googleapis.com/youtube/v3/channels?part=statisitcs&id={self.channel_id}&key={self.api_key}'
        print(url)

API_KEY = 'I cannot share my api key so I am not showing it but it is in my code'
yt = YTstats(API_KEY, 'UCbXgNpp0jedKWcQiULLbDTA')
yt.get_channel_statistics()

你这里有一个错字 part=statisitcs - 正如你在 中提到的 - 在仔细查看你在问题中提供的代码之后。

下次,请仔细检查您的 codd 并尝试使用 YouTube 数据 API 文档中的 try-it demo 功能重现错误。


我确实得到了您提供的 channel_id 的统计信息 - 即:UCbXgNpp0jedKWcQiULLbDTA:

这些是他们的统计数据:

"statistics": {
    "viewCount": "5642720",
    "subscriberCount": "98400",
    "hiddenSubscriberCount": false,
    "videoCount": "158"
  }
}

See demo

完整回复如下:

{
  "kind": "youtube#channelListResponse",
  "etag": "lG-nYlbLnN81gtjVKe1zKPW6v7A",
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 5
  },
  "items": [
    {
      "kind": "youtube#channel",
      "etag": "j4Fo8qKbWrLHnQYB8sCI8_I4v9A",
      "id": "UCbXgNpp0jedKWcQiULLbDTA",
      "snippet": {
        "title": "Python Engineer",
        "description": "Free Python and Machine Learning Tutorials!\n\nHi, I'm Patrick. I’m a passionate Software Engineer who loves Machine Learning, Computer Vision, and Data Science. I create free content in order to help more people get into those fields. If you have any questions, feedback, or comments, just shoot me a message! I am happy to talk to you :)\n\nIf you like my content, please subscribe to the channel!\n\nPlease check out my website for more information:\nhttps://www.python-engineer.com\n\nIf you find these videos useful and would like to support my work you can find me on Patreon:\nhttps://www.patreon.com/patrickloeber\n\nLegal: https://www.python-engineer.com/legal-notice/\n",
        "customUrl": "pythonengineer",
        "publishedAt": "2019-05-03T11:22:33Z",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AKedOLTs-Pvd4mvUi-m2rDLd8bzrKwS5a8C9HnDbkUDzHw=s88-c-k-c0x00ffffff-no-rj",
            "width": 88,
            "height": 88
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AKedOLTs-Pvd4mvUi-m2rDLd8bzrKwS5a8C9HnDbkUDzHw=s240-c-k-c0x00ffffff-no-rj",
            "width": 240,
            "height": 240
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AKedOLTs-Pvd4mvUi-m2rDLd8bzrKwS5a8C9HnDbkUDzHw=s800-c-k-c0x00ffffff-no-rj",
            "width": 800,
            "height": 800
          }
        },
        "localized": {
          "title": "Python Engineer",
          "description": "Free Python and Machine Learning Tutorials!\n\nHi, I'm Patrick. I’m a passionate Software Engineer who loves Machine Learning, Computer Vision, and Data Science. I create free content in order to help more people get into those fields. If you have any questions, feedback, or comments, just shoot me a message! I am happy to talk to you :)\n\nIf you like my content, please subscribe to the channel!\n\nPlease check out my website for more information:\nhttps://www.python-engineer.com\n\nIf you find these videos useful and would like to support my work you can find me on Patreon:\nhttps://www.patreon.com/patrickloeber\n\nLegal: https://www.python-engineer.com/legal-notice/\n"
        }
      },
      "contentDetails": {
        "relatedPlaylists": {
          "likes": "",
          "uploads": "UUbXgNpp0jedKWcQiULLbDTA"
        }
      },
      "statistics": {
        "viewCount": "5642720",
        "subscriberCount": "98400",
        "hiddenSubscriberCount": false,
        "videoCount": "158"
      }
    }
  ]
}

尝试在您拨打电话后等待几分钟 - 可能是 API 由于请求过多而无法检索您请求的数据,或者频道本身没有公开统计数据。

此问题现已修复(这是一个错字)