【问题标题】:Where do you specify your API key when making a request with the Google API python library?使用 Google API python 库发出请求时,您在哪里指定 API 密钥?
【发布时间】:2019-12-23 04:13:05
【问题描述】:

我开始研究将 YouTube API 与 python 结合使用,但文档中给出的示例似乎与快速入门指南不一致。

可以在here 找到快速入门指南,他们建议您从 API 文档here 中获取一些示例代码,这些代码将从 API 请求“YouTube 开发者”频道的一些信息。现在在快速入门指南中,他们说用您的 API 密钥替换“YOUR_API_KEY”字符串,但您可以从示例代码中看到它不存在。

# -*- coding: utf-8 -*-

# Sample Python code for youtube.channels.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python

import os

import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.channels().list(
        part="snippet,contentDetails,statistics",
        id="UC_x5XG1OV2P6uZZ5FSM9Ttw"
    )
    response = request.execute()

    print(response)

if __name__ == "__main__":
    main()

我相信“YOUR_CLIENT_SECRET_FILE”字符串仅适用于需要用户身份验证的请求,这种类型的请求不应该是这种情况。

那么您应该在哪里提供 API 密钥?指南过时了吗?

非常感谢所有帮助!

【问题讨论】:

    标签: python youtube-data-api


    【解决方案1】:

    我从来没有让未经授权的 API 密钥工作,所以我最终改用 OAuth 2.0。

    从 Google API 控制台转到凭据面板。如果您尚未创建客户端 ID,请将其下载到与您正在运行的 Python 脚本相同的目录中。

    打开您的 Python 脚本,找到 client_secrets_file 字符串。将“YOUR_CLIENT_SECRET_FILE.json”更改为您刚刚下载的 .json 文件的文件名。

    当您运行脚本时,它会要求您在浏览器中打开一个链接以完成授权过程。完成它,它应该按预期运行。希望这会有所帮助。

    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"
    

    【讨论】:

      【解决方案2】:
      • 您希望通过 YouTube Data API v3 使用 API 密钥检索频道列表。
        • 您已经拥有使用 YouTube Data API v3 的 API。
      • 您想用 Python 实现这个 google-api-python-client。
      • 您已经能够使用 YouTube Data API v3。

      如果我的理解是正确的,这个示例脚本怎么样?

      示例脚本:

      在您使用此脚本之前,请设置您的 API 密钥。

      from googleapiclient.discovery import build
      
      api_key = "###"  # Please set your API key
      
      api_service_name = "youtube"
      api_version = "v3"
      youtube = build(api_service_name, api_version, developerKey=api_key)
      request = youtube.channels().list(
          part="snippet,contentDetails,statistics",
          id="UC_x5XG1OV2P6uZZ5FSM9Ttw"
      )
      response = request.execute()
      print(response)
      

      注意:

      • 此示例脚本假设您已经能够使用 YouTube Data API v3。如果出现API相关的错误,请确认API是否开启。

      参考:

      如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

      【讨论】:

      • Google 文档在哪里没有指定使用 API 密钥的简单代码。非常感谢!!
      猜你喜欢
      • 1970-01-01
      • 2011-09-28
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 2022-01-12
      • 1970-01-01
      相关资源
      最近更新 更多