python - 如何修复此错误 - 使用 YouTube API 凭据通过关键字请求 YouTube 搜索时出现 HttpError 403
问题描述
我似乎收到了这个错误,但不能完全确定问题所在。
我的 API Key 看起来不错,没有任何限制,而且我已经很久没有使用这个 API Key 了。当我几乎不使用它时,我看不出我是如何超过配额的。
#Import modules
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser
import pandas as pd
import pprint
import matplotlib.pyplot as plt
#Set up YouTube credentials
DEVELOPER_KEY = key.DEVELOPER_KEY
YOUTUBE_API_SERVICE_NAME = key.YOUTUBE_API_SERVICE_NAME
YOUTUBE_API_VERSION = key.YOUTUBE_API_VERSION
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,developerKey=key.DEVELOPER_KEY)
#-------------Build YouTube Search------------#
def youtubeSearch(query, max_results=50,order="relevance", token=None, location=None, location_radius=None):
#search upto max 50 videos based on query
search_response = youtube.search().list(
q=query,
type="video",
pageToken=token,
order = order,
part="id,snippet",
maxResults=max_results,
location=location,
locationRadius=location_radius).execute()
print("Search Completed...")
print("Total results: {0} \nResults per page: {1}".format(search_response['pageInfo']['totalResults'], search_response['pageInfo']['resultsPerPage']))
print("Example output per item, snippet")
print(search_response['items'][0]['snippet'].keys())
#Assign first page of results (items) to item variable
items = search_response['items'] #50 "items"
#Assign 1st results to title, channelId, datePublished then print
title = items[0]['snippet']['title']
channelId = items[0]['snippet']['channelId']
datePublished = items[0]['snippet']['publishedAt']
print("First result is: \n Title: {0} \n Channel ID: {1} \n Published on: {2}".format(title, channelId, datePublished))
return search_response
youtubeSearch("Drake Future")
出现此错误。
HttpError: <HttpError 403 when requesting https://youtube.googleapis.com/youtube/v3/search?q=Drake+Future&type=video&order=relevance&part=id%2Csnippet&maxResults=50&key=[MY API KEY]&alt=json "The request cannot be completed because you have exceeded your <a href="/youtube/v3/getting-started#quota">quota</a>.">
解决方案
推荐阅读
- r - 为什么使用 RScript 调用 R 脚本有效,但不适用于 R 二进制文件?
- com - 如何在 SourceSafe 中获取项目 (IVssItem) 的日期?
- python - 在 django 中创建/更新模型表单的最佳方法
- ios - SwiftUI ForEach 未更新 ObservableObject 类数组的以下更改
- github - 如何使用 ESP8266 上的令牌向 GitHub 进行身份验证?
- python - 操作类型未在桌面上运行的二进制文件中注册“MaxBytesInUse”
- python - 如何在 50 秒后杀死一个子进程?
- javascript - 如何检查子数组中的元素是否存在于数组的其他子数组中并通过第一个索引的值进行排序
- android - ActionBar 已经覆盖了页面的内容
- javascript - 调整屏幕大小:弄乱网站上的部分