【问题标题】:python yt api "The request is missing a valid API key."python yt api“请求缺少有效的 API 密钥。”
【发布时间】:2021-10-23 17:07:30
【问题描述】:

我在 python 中使用 youtube api,我使用这个代码:

   r = requests.get('https://www.googleapis.com/youtube/v3/videos')
   print(r.text)

我得到了这个输出:

{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "errors": [
      {
        "message": "The request is missing a valid API key.",
        "domain": "global",
        "reason": "forbidden"
      }
    ],
    "status": "PERMISSION_DENIED"
  }
}


Process finished with exit code 0

我已经有了我的 api 密钥,所以我的问题是如何将此 api 密钥添加到 GET 请求并使其工作

【问题讨论】:

    标签: python api youtube


    【解决方案1】:

    应将 API 密钥添加为名为 key 的 GET 参数,如下所示:

    r = requests.get('https://www.googleapis.com/youtube/v3/videos?key=YOUR_API_KEY&id=7lCDEYXw3mM&...')
    print(r.text)
    

    documentation 有更多示例。

    【讨论】:

    • id 是什么?
    • 这只是一个例子。此请求还有一些必填字段,都列在reference documentation 中,但key 是您询问的。
    【解决方案2】:

    好的,我的代码试图从以色列的 youtube 获取所有视频的列表,以便我的代码:

    r = requests.get('https://www.googleapis.com/youtube/v3/videos?key=AIzaSyC2-eKIWkTVIqEv1_oDVyton78YCIH5uio&id=')
    print(r.text)
    
    youtube_server = build('youtube', 'v3', developerKey=api_key)                                                       request = youtube_server.videos().list(part='snippet', regionCode='IL')
    response = request.execute()
    print(response)
    

    我得到这个错误:

      File "C:/Users/User/Downloads/YoutubeAPI.py", line 18, in <module>
        response = request.execute()
      File "C:\Users\User\PycharmProjects\FileRename\venv\lib\site-packages\googleapiclient\_helpers.py", line 131, in positional_wrapper
        return wrapped(*args, **kwargs)
      File "C:\Users\User\PycharmProjects\FileRename\venv\lib\site-packages\googleapiclient\http.py", line 937, in execute
        raise HttpError(resp, content, uri=self.uri)
    googleapiclient.errors.HttpError: <HttpError 400 when requesting https://youtube.googleapis.com/youtube/v3/videos?part=snippet&regionCode=IL&key=AIzaSyC2-eKIWkTVIqEv1_oDVyton78YCIH5uio&alt=json returned "No filter selected. Expected one of: chart, myRating, id". Details: "[{'message': 'No filter selected. Expected one of: chart, myRating, id', 'domain': 'youtube.parameter', 'reason': 'missingRequiredParameter', 'location': 'parameters.', 'locationType': 'other'}]"> 
    

    【讨论】:

      猜你喜欢
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多