【问题标题】:Error with determining credentials and the YouTube API确定凭据和 YouTube API 时出错
【发布时间】:2021-03-02 10:59:09
【问题描述】:

更新:修复:- 只是发现我的 .bashrc 缺少以下内容

您可以使用环境变量更新 bash 配置文件

if [ -f ~/.bash_profile ]; then\
   . ~/.bash_profile\
fi

我正在尝试来自 youtube 的 corey schafer 的 API 脚本。我最近将我的文本编辑器从 Sublime 文本更改为 VSCode。

我期望 Sublime 文本的输出如下,

{'kind': 'youtube#channelListResponse', 'etag': '-bp4b5Yy0e-HRWQsadmZk2A75GE', 'pageInfo': {'totalResults': 1, 'resultsPerPage': 5}, 'items': [{'kind': 'youtube#channel', 'etag': 'vakSZODpMKK8i9f3UStrRvd2sQA', 'id': 'UCCezIgC97PvUuR4_gbFUs5g', 'statistics': {'viewCount': '47863717', 'subscriberCount': '667000', 'hiddenSubscriberCount': False, 'videoCount': '230'}}]}
[Finished in 1.0s]

我收到以下错误,请注意我已将 python 别名为 python3 我是初学者。如果我缺少基本的东西,请告诉我

python -u "/Users/natluri/Documents/ytproj/youtubeapi.py"
Traceback (most recent call last):
  File "/Users/natluri/Documents/ytproj/youtubeapi.py", line 5, in <module>
    youtube = build('youtube', 'v3', developerKey=api_key)
  File "/usr/local/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/googleapiclient/discovery.py", line 278, in build
    service = build_from_document(
  File "/usr/local/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/googleapiclient/discovery.py", line 527, in build_from_document
    credentials = _auth.default_credentials(
  File "/usr/local/lib/python3.9/site-packages/googleapiclient/_auth.py", line 54, in default_credentials
    credentials, _ = google.auth.default(scopes=scopes, quota_project_id=quota_project_id)
  File "/usr/local/lib/python3.9/site-packages/google/auth/_default.py", line 356, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started

【问题讨论】:

  • 您的 google 凭据文件似乎丢失了。
  • 无法自动确定凭据。请设置 GOOGLE_APPLICATION_CREDENTIALS 或显式创建凭据并重新运行应用程序。 接缝非常不言自明。

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


【解决方案1】:

由于您没有发布代码,因此很难帮助您修复代码,但是以下应该是如何在不使用 env var 的情况下授权 YouTube API 的示例。

SCOPES = ['https://www.googleapis.com/auth/youtube.readonly']
CLIENT_SECRETS_PATH = 'client_secrets.json' # Path to client_secrets.json file.
VIEW_ID = '<REPLACE_WITH_VIEW_ID>'


def initialize_youtube():
  """Initializes the youtube service object.

  Returns:
    analytics an authorized youtube service object.
  """
  # Parse command-line arguments.
  parser = argparse.ArgumentParser(
      formatter_class=argparse.RawDescriptionHelpFormatter,
      parents=[tools.argparser])
  flags = parser.parse_args([])

  # Set up a Flow object to be used if we need to authenticate.
  flow = client.flow_from_clientsecrets(
      CLIENT_SECRETS_PATH, scope=SCOPES,
      message=tools.message_if_missing(CLIENT_SECRETS_PATH))

  # Prepare credentials, and authorize HTTP object with them.
  # If the credentials don't exist or are invalid run through the native client
  # flow. The Storage object will ensure that if successful the good
  # credentials will get written back to a file.
  storage = file.Storage('youtube.dat')
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    credentials = tools.run_flow(flow, storage, flags)
  http = credentials.authorize(http=httplib2.Http())

  # Build the service object.
  youtube = build('youtube', 'v3', http=http)

  return youtube

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-08
    • 2016-03-18
    • 2018-01-05
    • 2019-08-22
    • 2018-08-28
    • 1970-01-01
    • 2016-06-02
    • 2020-10-25
    相关资源
    最近更新 更多