首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >oauth2client现在已弃用

oauth2client现在已弃用
EN

Stack Overflow用户
提问于 2019-04-04 21:52:06
回答 1查看 620关注 0票数 0

在通过API从Google Analytics ( https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py )请求数据的Python代码中,使用了oauth2client。该代码最后一次更新是在2018年7月,到目前为止,oauth2client一直处于弃用状态。我的问题是,我能得到相同的代码吗,在哪里使用google-auth或oauthlib而不是oauth2client?

我在谷歌上寻找一个解决方案,如何替换使用oauth2client的代码部分。然而,由于我不是一名开发人员,我没有成功。这就是我尝试将此链接( https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py )中的代码改编为google-auth的方法。你知道怎么解决这个问题吗?

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

发布于 2020-03-28 09:27:07

对于那些遇到这个问题并希望移植到更新的身份验证库的人,在G Suite APIs intro codelabcode repo上对两个不同版本的简短/简单Google Drive API示例进行比较,看看哪些需要更新(以及哪些可以保持原样)。底线是API客户端库代码可以保持不变,而您所要做的就是换出下面的auth库。

请注意,示例仅适用于用户帐户验证...对于svc acct auth,更新是类似的,但我还没有这样的例子(虽然正在工作中……一旦发布,就会更新它)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55517672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档