我正在使用 Python 為一個專案撰寫代碼。我正在嘗試制作一個專案,當我遇到錯誤時,當你最喜歡的 youtuber 上傳他/她的視頻時,它會告訴你。
這是我的代碼:
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
請幫助我解決這個問題,因為這對我來說意味著世界。????謝謝。
uj5u.com熱心網友回復:
如果您查看完整回復,您將看到:
{
"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,請等待幾分鐘讓操作傳播到我們的系統并重試。”
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418985.html
標籤:
