在不生成服务帐户client_secrets的情况下访问Google Analytics(分析)API >>

问题描述 投票:0回答:1

我正在尝试访问客户端的Google Analytics(分析)报告(v4),因此无法生成自己的client_secrets.json文件。过去,我曾使用过oauth2client库来创建流和存储对象以通过http进行身份验证。当前的google文档仍使用oauth2client库,根据pypi.org(和google),该库已于2018年9月弃用。

这是我们用于构建服务的当前代码:

import argparse
from apiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools


def get_service(api_name, api_version, scope, client_secrets_path):
  """Get a service that communicates to a Google API.

  Args:
    api_name: string The name of the api to connect to.
    api_version: string The api version to connect to.
    scope: A list of strings representing the auth scopes to authorize for the
      connection.
    client_secrets_path: string A path to a valid client secrets file.

  Returns:
    A service that is connected to the specified API.
  """
  # 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=scope,
      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(api_name + '.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.
  service = build(api_name, api_version, http=http)

  return service

当前的Google文档可以在这里找到:https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/installed-py

如何在不从客户端接收特定的client_secrets且不使用oauth2client的情况下创建此内部版本?

我正在尝试访问客户端的Google Analytics(分析)报告(v4),因此无法生成自己的client_secrets.json文件。过去我曾使用过oauth2client库来创建流和存储...

python google-cloud-platform google-analytics google-oauth google-oauth2
1个回答
0
投票

[如果您使用service account flow,您需要做的就是要求您的客户端向您需要访问的分析帐户/媒体资源/视图添加其他用户(这只是一个api帐户连接),并且意味着您不需要构造oauth2流。

© www.soinside.com 2019 - 2024. All rights reserved.