urllib.error.HTTPError:HTTP错误403:禁止错误



我正在使用Python为一个项目编写代码。当我遇到错误时,我正试图做一个项目,当你最喜欢的youtuber上传他/她的视频时,告诉你。

这是我的代码:

import time
import urllib.request, json
def look_for_new_video():
api_key = "AIzaSy..."
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:UsersgarviOneDriveDesktopcode-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:UsersgarviOneDriveDesktopcode-learningpy1main.py", line 25, in <module>
look_for_new_video()
File "c:UsersgarviOneDriveDesktopcode-learningpy1main.py", line 12, in look_for_new_video
inp = urllib.request.urlopen(url)
File "C:UsersgarviAppDataLocalProgramsPythonPython310liburllibrequest.py", line 216, in urlopen
return opener.open(url, data, timeout)
File "C:UsersgarviAppDataLocalProgramsPythonPython310liburllibrequest.py", line 525, in open
response = meth(req, response)
File "C:UsersgarviAppDataLocalProgramsPythonPython310liburllibrequest.py", line 634, in http_response
response = self.parent.error(
File "C:UsersgarviAppDataLocalProgramsPythonPython310liburllibrequest.py", line 563, in error
return self._call_chain(*args)
File "C:UsersgarviAppDataLocalProgramsPythonPython310liburllibrequest.py", line 496, in _call_chain
result = func(*args)
File "C:UsersgarviAppDataLocalProgramsPythonPython310liburllibrequest.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 Data API v3以前未在项目169818930380中使用过,或者已禁用。通过访问启用https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=169818930380然后重试。如果您最近启用了此API,请等待几分钟,等待该操作传播到我们的系统,然后重试">

相关内容

最新更新