【问题标题】:The request is missing a Valid API Key with service account authorization请求缺少具有服务帐户授权的有效 API 密钥
【发布时间】:2021-04-28 19:39:41
【问题描述】:

我正在尝试将 youtube cmets 拉入数据框。我可以提取 youtube 视频的详细信息,但是当我尝试接收 commentThreads 资源时收到 403, "The request is missing a valid API key." 错误。

def get_service(api_name, api_version, scopes, key_file_location):
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
            key_file_location, scopes=scopes)

    # Build the service object.
    service = build(api_name, api_version, credentials=credentials)

    return service

# Authenticate and construct service.
service = get_service(
        api_name='youtube',
        api_version='v3',
        scopes='https://www.googleapis.com/auth/youtube.force-ssl',
        key_file_location='key_file.json')

key_file.json 是服务帐户密钥凭据。 这是我得到错误的地方:

    response = service.commentThreads().list(
                      part = 'snippit',
                      videoId = video_id,
                      maxResults = 100,
                      order = 'relevance',
                      textFormat = 'plainText',
                      pageToken = nextPage_token).execute()

我不知道为什么我在这里收到一个禁止的错误,因为当我执行以下功能时,它工作正常:

query_results = service.search().list(part = 'snippet', q = query,
                      order = 'relevance',
                      type = 'video',
                      relevanceLanguage = 'en',
                      safeSearch = 'moderate',).execute()

【问题讨论】:

  • 您有一个错字:将snippit 替换为snippet
  • key_file.json 是否与您正在执行的 Python 脚本位于同一文件夹中?

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


【解决方案1】:

凭证 = ServiceAccountCredentials.from_json_keyfile_name( key_file_location,范围=范围)

您似乎正在尝试通过 YouTube API 使用服务帐户身份验证。 YouTube API 不支持您需要使用 Oauth2 对用户进行身份验证的服务帐户。

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

  Returns:
    youtube 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

【讨论】:

    猜你喜欢
    • 2018-12-25
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2023-03-31
    • 2018-11-07
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    相关资源
    最近更新 更多