Python oauth2client现在已弃用

Python oauth2client现在已弃用,python,credentials,google-auth-library,requests-oauthlib,Python,Credentials,Google Auth Library,Requests Oauthlib,在通过API从Google Analytics()请求数据的Python代码中,使用了oauth2client。该代码上次更新是在2018年7月,到目前为止,oauth2client已被弃用。我的问题是,我能得到相同的代码吗?在哪里使用的不是oauth2client,而是google auth或oauthlib 我在谷歌上搜索如何替换使用oauth2client的代码部分。然而,由于我不是一名开发人员,我没有成功。这就是我试图将此链接()中的代码改编为GoogleAuth的方式。你知道怎么解决这

在通过API从Google Analytics()请求数据的Python代码中,使用了oauth2client。该代码上次更新是在2018年7月,到目前为止,oauth2client已被弃用。我的问题是,我能得到相同的代码吗?在哪里使用的不是oauth2client,而是google auth或oauthlib

我在谷歌上搜索如何替换使用oauth2client的代码部分。然而,由于我不是一名开发人员,我没有成功。这就是我试图将此链接()中的代码改编为GoogleAuth的方式。你知道怎么解决这个问题吗

import argparse

from apiclient.discovery import build
from google.oauth2 import service_account
from google.auth.transport.urllib3 import AuthorizedHttp


SCOPES = ['...']
DISCOVERY_URI = ('...')
CLIENT_SECRETS_PATH = 'client_secrets.json' # Path to client_secrets.json file.
VIEW_ID = '...'


def initialize_analyticsreporting():
  """Initializes the analyticsreporting service object.

  Returns:l
    analytics an authorized analyticsreporting service object.
  """
  # Parse command-line arguments.
  credentials = service_account.Credentials.from_service_account_file(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.
authed_http = AuthorizedHttp(credentials)

response = authed_http.request(
    'GET', SCOPES)

  # Build the service object.
  analytics = build('analytics', 'v4', http=http, discoveryServiceUrl=DISCOVERY_URI)

  return analytics

def get_report(analytics):
  # Use the Analytics Service Object to query the Analytics Reporting API V4.
  return analytics.reports().batchGet(
      body=
    {
    "reportRequests":[
    {
      "viewId":VIEW_ID,
      "dateRanges":[
      {
        "startDate":"2019-01-01",
        "endDate":"yesterday"
      }],
      "dimensions":[
      {
        "name":"ga:transactionId"
      },
      {
        "name":"ga:sourceMedium"
      },
      {
        "name":"ga:date"
      }],
      "metrics":[
      {
        "expression":"ga:transactionRevenue"
      }]
    }]
  }
).execute()


def printResults(response):
  for report in response.get("reports", []):
    columnHeader = report.get("columnHeader", {})
    dimensionHeaders = columnHeader.get("dimensions", [])
    metricHeaders = columnHeader.get("metricHeader", {}).get("metricHeaderEntries", [])
    rows = report.get("data", {}).get("rows", [])

    for row in rows:
      dimensions = row.get("dimensions", [])
      dateRangeValues = row.get("metrics", [])

      for header, dimension in zip(dimensionHeaders, dimensions):
        print (header + ": " + dimension)

      for i, values in enumerate(dateRangeValues):
        for metric, value in zip(metricHeaders, values.get("values")):
          print (metric.get("name") + ": " + value)


def main():

  analytics = initialize_analyticsreporting()
  response = get_report(analytics)
  printResults(response)

if __name__ == '__main__':
  main()

I need to obtain response in form of a json with given dimensions and metrics from Google Analytics.

对于遇到此问题并希望移植到较新的auth库的用户,请在上对两个不同版本的short/simple Google Drive API示例进行比较,以查看需要更新的内容(以及可以保持原样的内容)。底线是,API客户机库代码可以保持不变,而您所做的只是交换下面的auth库

请注意,此示例仅适用于用户帐户验证。。。对于svc acct auth,更新是类似的,但我还没有这样的例子(尽管正在研究一个……一旦发布就会更新)