在通过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的方法。你知道怎么解决这个问题吗?
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.发布于 2020-03-28 09:27:07
对于那些遇到这个问题并希望移植到更新的身份验证库的人,在G Suite APIs intro codelab的code repo上对两个不同版本的简短/简单Google Drive API示例进行比较,看看哪些需要更新(以及哪些可以保持原样)。底线是API客户端库代码可以保持不变,而您所要做的就是换出下面的auth库。
请注意,示例仅适用于用户帐户验证...对于svc acct auth,更新是类似的,但我还没有这样的例子(虽然正在工作中……一旦发布,就会更新它)。
https://stackoverflow.com/questions/55517672
复制相似问题