首页 > 解决方案 > 如何修复此错误 - 使用 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>.">

标签: pythonpython-3.xgoogle-apiyoutubeyoutube-data-api

解决方案


推荐阅读