Python 确定凭据和YouTube API时出错

Python 确定凭据和YouTube API时出错,python,google-api,youtube-api,google-api-python-client,Python,Google Api,Youtube Api,Google Api Python Client,更新:修复:- 只是觉得我错过了我的.bashrc 您可以使用环境变量源代码更新bash概要文件 if [ -f ~/.bash_profile ]; then\ . ~/.bash_profile\ fi 我正在尝试来自youtube的corey schafer的API脚本。我最近将我的文本编辑器从Sublime文本更改为VSCode 我期望从Sublime文本中获得如下输出: {'kind': 'youtube#channelListResponse', 'etag': '-bp4b

更新:修复:- 只是觉得我错过了我的.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
python-u”/Users/natluri/Documents/ytproj/youtubeapi.py“
回溯(最近一次呼叫最后一次):
文件“/Users/natluri/Documents/ytproj/youtubeapi.py”,第5行,在
youtube=build('youtube','v3',developerKey=api_)
位置包装中的文件“/usr/local/lib/python3.9/site packages/googleapiclient/_helpers.py”,第134行
已包装退货(*args,**kwargs)
文件“/usr/local/lib/python3.9/site packages/googleapiclient/discovery.py”,第278行,内部版本
服务=根据文档生成(
位置包装中的文件“/usr/local/lib/python3.9/site packages/googleapiclient/_helpers.py”,第134行
已包装退货(*args,**kwargs)
文件“/usr/local/lib/python3.9/site packages/googleapiclient/discovery.py”,第527行,来自文档的内部版本
凭据=\u auth.default\u凭据(
默认凭据中的文件“/usr/local/lib/python3.9/site packages/googleapiclient/_auth.py”,第54行
凭据,\=google.auth.default(范围=范围,配额\项目\ id=配额\项目\ id)
文件“/usr/local/lib/python3.9/site packages/google/auth/_default.py”,默认为第356行
引发异常。DefaultCredentialsError(\u帮助\u消息)
google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据。请设置google_应用程序_凭据或显式创建凭据并重新运行应用程序。有关详细信息,请参阅https://cloud.google.com/docs/authentication/getting-started

由于您没有发布代码,因此很难帮助您修复代码,但是下面应该是一个示例,说明如何在不使用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
SCOPES=['https://www.googleapis.com/auth/youtube.readonly']
CLIENT_SECRETS_PATH='CLIENT_SECRETS.json'#CLIENT_SECRETS.json文件的路径。
视图ID=“”
def initialize_youtube():
“”初始化youtube服务对象。
返回:
分析一个授权的youtube服务对象。
"""
#解析命令行参数。
parser=argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
父项=[tools.argparser])
flags=parser.parse_args([])
#如果需要进行身份验证,请设置要使用的流对象。
flow=client.flow\u from\u clientsecrets(
客户机路径,范围=范围,
message=tools.message\u如果缺少(客户端\u秘密\u路径))
#准备凭据,并使用它们授权HTTP对象。
#如果凭据不存在或无效,请通过本机客户端运行
#存储对象将确保如果成功
#凭据将被写回文件。
storage=file.storage('youtube.dat')
凭据=存储。获取()
如果凭据为无或凭据无效:
凭据=工具。运行\u流(流、存储、标志)
http=credentials.authorize(http=httplib2.http())
#构建服务对象。
youtube=build('youtube','v3',http=http)
返回youtube

您的google凭据文件似乎丢失。无法自动确定凭据。请设置google\u应用程序\u凭据或显式创建凭据,然后重新运行应用程序。这是不言自明的。