urllib.error.HTTPError: HTTP Error 403: Forbidden Error

urllib.error.HTTPError: HTTP Error 403: Forbidden Error

我正在使用 Python 为一个项目编写代码。当我遇到错误时,我正在尝试制作一个项目,当您最喜欢的 youtuber 上传 his/her 视频时告诉您。

这是我的代码:

import time
import urllib.request, json

def look_for_new_video():
    api_key = "AIzaSyC-t1EEyBswGE2SS0Nz8eFSKrLjbhAOO7E"
    channel_id = "UCd4QrSA9GDlViP_0dwbdSBA"

    base_video_url = "https://www.youtube.com/watch?v="
    base_search_url = "https://www.googleapis.com/youtube/v3/search?"

    url = base_search_url + 'key={}&channelId={}&part=snippet,id&order=date&maxResults=1'.format(api_key, channel_id)
    inp = urllib.request.urlopen(url)
    resp = json.load(inp)

    vidID = resp['items'][0]['id']['videoId']
    video_exists = False
    with open('videoid.json', 'r') as json_file:
        data = json.load(json_file)
        if data['videoId'] != vidID:
            data = {'videoId':vidID, 'channelId':channel_id}
            json.dump(data, json_file)

try:
    while True:
        look_for_new_video()
        time.sleep(10)

except KeyboardInterrupt:
    print('stopping')

这是我遇到的错误:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS C:\Users\garvi\OneDrive\Desktop\code-learning> & C:/Users/garvi/AppData/Local/Programs/Python/Python310/python.exe c:/Users/garvi/On
eDrive/Desktop/code-learning/py1/main.py
Traceback (most recent call last):
  File "c:\Users\garvi\OneDrive\Desktop\code-learning\py1\main.py", line 25, in <module>
    look_for_new_video()
  File "c:\Users\garvi\OneDrive\Desktop\code-learning\py1\main.py", line 12, in look_for_new_video
    inp = urllib.request.urlopen(url)
  File "C:\Users\garvi\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\garvi\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 525, in open
    response = meth(req, response)
  File "C:\Users\garvi\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 634, in http_response
    response = self.parent.error(
  File "C:\Users\garvi\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 563, in error
    return self._call_chain(*args)
  File "C:\Users\garvi\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 496, in _call_chain
    result = func(*args)
  File "C:\Users\garvi\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 643, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

请帮我解决这个问题,因为这对我来说意义重大。谢谢

如果您查看完整回复,您将看到:

{
  "error": {
    "code": 403,
    "message": "YouTube Data API v3 has not been used in project 169818930380 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=169818930380 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "errors": [
      {
        "message": "YouTube Data API v3 has not been used in project 169818930380 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=169818930380 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
        "domain": "usageLimits",
        "reason": "accessNotConfigured",
        "extendedHelp": "https://console.developers.google.com"
      }
    ],
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developers console API activation",
            "url": "https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=169818930380"
          }
        ]
      },
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "SERVICE_DISABLED",
        "domain": "googleapis.com",
        "metadata": {
          "consumer": "projects/169818930380",
          "service": "youtube.googleapis.com"
        }
      }
    ]
  }
}

因此您必须按照以下说明进行操作:“YouTube 数据 API v3 之前未在项目 169818930380 中使用过或已被禁用。通过访问 https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=169818930380 启用它然后重试。如果您启用这个 API 最近,请等待几分钟让操作传播到我们的系统并重试。"