我正在使用Google Analytics(分析)API提取数据。我想将一年的数据转储到csv文件中。我也实现了拆分,以处理大数据。当我运行代码时,它开始将数据转储到csv中。但是,它崩溃并显示以下消息。每次我运行代码时都会发生这种情况。 此外,必须注意,我尝试提取数据1天,但仍然遇到相同的错误
googleapiclient.errors.HttpError:
https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json时返回“该服务当前不可用。“>
以下是我的代码。在这方面的任何帮助将不胜感激
import httplib2 as lib2
import google.oauth2.credentials
from google_auth_httplib2 import AuthorizedHttp
from datetime import datetime
import psycopg2
#Packages needed for connecting with Google API
from googleapiclient.discovery import build as google_build
#Data processing packages
import pandas
import numpy
import json
from datetime import datetime, timedelta
access_token = "***********"
refresh_token = "**********"
client_id = "***********"
client_secret = "*************"
token_uri = 'https://oauth2.googleapis.com/token'
token_expiry = datetime.now() - timedelta(days = 1)
#¯\_(ツ)_/¯
user_agent = 'my-user-agent/1.0'
credentials = google.oauth2.credentials.Credentials(access_token,
refresh_token=refresh_token,
token_uri='https://oauth2.googleapis.com/token',
client_id=client_id,
client_secret=client_secret)
#Authorize client
authorized = AuthorizedHttp(credentials=credentials)
api_name = 'analyticsreporting'
api_version = 'v4'
#Let's build the client
api_client_1dayactiveusers = google_build(serviceName=api_name, version=api_version, http=authorized)
pageToken_1dayactiveusers='firstcall'
# for user types
while pageToken_1dayactiveusers != None:
sample_request = {
'viewId': '**********',
'dateRanges': {
'startDate': datetime.strftime(datetime.now() - timedelta(days = 365),'%Y-%m-%d'),
'endDate': datetime.strftime(datetime.now(),'%Y-%m-%d')
},
'dimensions': [{'name': 'ga:date'}],
'metrics': [{'expression': 'ga:1dayUsers','alias':'onedayusers'}],
'pageToken': pageToken_1dayactiveusers
}
response_1dayactiveusers = api_client_1dayactiveusers.reports().batchGet(
body={
'reportRequests': sample_request
}).execute()
print(response_1dayactiveusers)
pageToken = response_1dayactiveusers.get("reports")[0].get('nextPageToken', None)
print(pageToken)
def parse_response(report):
"""Parses and prints the Analytics Reporting API V4 response"""
# Initialize results, in list format because two dataframes might return
result_list = []
# Initialize empty data container for the two dateranges (if there are two that is)
data_csv = []
data_csv2 = []
# Initialize header rows
header_row = []
# Get column headers, metric headers, and dimension headers.
columnHeader = report.get('columnHeader', {})
metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
dimensionHeaders = columnHeader.get('dimensions', [])
# Combine all of those headers into the header_row, which is in a list format
for dheader in dimensionHeaders:
header_row.append(dheader)
for mheader in metricHeaders:
header_row.append(mheader['name'])
# Get data from each of the rows, and append them into a list
rows = report.get('data', {}).get('rows', [])
for row in rows:
row_temp = []
dimensions = row.get('dimensions', [])
metrics = row.get('metrics', [])
for d in dimensions:
row_temp.append(d)
for m in metrics[0]['values']:
row_temp.append(m)
data_csv.append(row_temp)
# Putting those list formats into pandas dataframe, and append them into the final result
result_df = pandas.DataFrame(data_csv, columns=header_row)
result_list.append(result_df)
return result_list
response_data = response_1dayactiveusers.get('reports', [])[0]
df = parse_response(response_data)[0]
df.to_csv('/Users/ga_csv_2.csv', mode='a', header=False)