【问题标题】:google-api-python-client doesn't work with developer keygoogle-api-python-client 不适用于开发人员密钥
【发布时间】:2017-05-22 22:53:42
【问题描述】:

我无法让这个脚本工作。我将 DEVELOPER_KEY 替换为从 Google API 创建的 API 密钥。如果有人可以帮助我,我将不胜感激。脚本来自这个页面:https://developers.google.com/youtube/v3/code_samples/python#search_by_keyword

#!/usr/bin/python

from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser


# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of
#   https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
DEVELOPER_KEY = "REPLACE_ME"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)

我收到以下错误消息。

Traceback (most recent call last):
  File "hello.py", line 16, in <module>
    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
  File "/home/alican/.local/lib/python2.7/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/alican/.local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 226, in build
    credentials=credentials)
  File "/home/alican/.local/lib/python2.7/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/alican/.local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 358, in build_from_document
    credentials = _auth.default_credentials()
  File "/home/alican/.local/lib/python2.7/site-packages/googleapiclient/_auth.py", line 40, in default_credentials
    return oauth2client.client.GoogleCredentials.get_application_default()
  File "/home/alican/.local/lib/python2.7/site-packages/oauth2client/client.py", line 1264, in get_application_default
    return GoogleCredentials._get_implicit_credentials()
  File "/home/alican/.local/lib/python2.7/site-packages/oauth2client/client.py", line 1254, in _get_implicit_credentials
    raise ApplicationDefaultCredentialsError(ADC_HELP_MSG)
oauth2client.client.ApplicationDefaultCredentialsError: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

【问题讨论】:

  • 你去推荐页面看了吗?您是否设置了适当的环境变量?
  • 是的,但问题是我不想使用 OAuth 2.0 访问 Google API,我只想使用 API 密钥。这个脚本实际上在几周前运行良好。
  • 现在可以使用了,谢谢。我意识到我误解了说明。
  • @alicankargin ,我面临同样的问题。该脚本在几周前运行良好。你是怎么解决的。你能发布你的解决方案并接受它作为答案吗

标签: python youtube-data-api google-api-python-client


【解决方案1】:

如果您想使用 google-api-python-client 1.6 版,请执行以下操作。 转到https://console.developers.google.com/apis/credentials。选择创建凭据,选择服务帐户密钥,选择 App Engine 默认服务帐户,下载 JSON 文件并将其放入您的项目文件夹中。然后将它设置为环境变量GOOGLE_APPLICATION_CREDENTIALS

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./google_application_credentials.json"

【讨论】:

    【解决方案2】:

    这是 1.6 版 google-api-python-client 的问题。使用以前的版本(1.5.5)应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-18
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 2015-12-31
      • 2012-08-31
      • 1970-01-01
      相关资源
      最近更新 更多