TypeError:('__ init __()至少需要3个参数(给定1个)',<class'googleapiclient.errors.httperror'=“”>,())

时间:2016-12-12 08:37:39

标签: python google-analytics-api google-api-python-client service-accounts

我使用python从GA API获取数据。这是我的代码:

    # -*- coding: utf-8 -*-
"""Hello Analytics Reporting API V4."""

import argparse
import pandas as pd
import time

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools
from pandas import Series, DataFrame
from multiprocessing import Pool
from functools import partial


SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
DISCOVERY_URI = ('https://analyticsreporting.googleapis.com/$discovery/rest')
KEY_FILE_LOCATION = '/Users/mac/Documents/python-f/client_secrets.p12'
SERVICE_ACCOUNT_EMAIL = 'mac-test3@prime-imagery-150409.iam.gserviceaccount.com'
VIEW_ID = '108573919'
FILE_NAME = '/Users/mac/Documents/python-f/12ios_bbs_user.csv'
DATE_RANGE = pd.date_range('1/1/2016','12/11/2016')
FILTERS = 'ga:screenName=~论坛'
SEGMENTS_ID = 'gaid::gKXoOymDSxKWjmKVPpPsbA'


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

  Returns:
    analytics an authorized analyticsreporting service object.
  """

  credentials = ServiceAccountCredentials.from_p12_keyfile(
    SERVICE_ACCOUNT_EMAIL, KEY_FILE_LOCATION, scopes=SCOPES)

  http = credentials.authorize(httplib2.Http())

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

  return analytics


def get_report(q_date):
  # Use the Analytics Service Object to query the Analytics Reporting API V4.
  analytics = initialize_analyticsreporting()
  return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': q_date, 'endDate': q_date}],
          'metrics': [{'expression': 'ga:screenviews'},{'expression': 'ga:users'}],
          'dimensions': [{'name': 'ga:date'},{'name': 'ga:segment'}],
          #'dimensions': [{'name': 'ga:date'}]
          'filtersExpression': FILTERS
          #'segments':  [{'segmentId': SEGMENTS_ID}]
        }]
      }
  ).execute()


def print_response(response):
  content=[]
  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', [])
    hed_r = []
    con_r = []

    mer_h = []
    for m in metricHeaders:
      mer_h.append(m.get('name'))

    hed_r = dimensionHeaders + mer_h

    con_r.append(hed_r)

    for row in rows:
      dimensions = row.get('dimensions', [])
      dateRangeValues = row.get('metrics', [])
      line=[]
      for di in dimensions:
        line.append(di)
      for values in dateRangeValues:
        for value in values.get('values'):
          line.append(value)
      con_r.append(line)
    content.append(con_r)
  df = DataFrame(content[0])
  df.columns = df.ix[0]

  return df[1:]


def main():

 #analytics1 = initialize_analyticsreporting()
 #startTime = time.time()
  results = []
  date_range = []

  for t in DATE_RANGE:
    date_range.append(t.strftime('%Y-%m-%d'))

  pool = Pool()
  rrr=pool.map(get_report, date_range)
  pool.close()
  pool.join()

  for rlt in rrr:
    rlt = print_response(rlt)
    results.append(rlt)

  results = pd.concat(results, ignore_index=True)
 #endTime = time.time()
  results.to_csv(FILE_NAME,encoding='utf-8')




  '''
  for i, t in enumerate(DATE_RANGE):
    rlt = get_report(analytics, t.strftime('%Y-%m-%d'))
    rlt_r = print_response(rlt)
    if i==0:
      results = rlt_r
    else:
      results = pd.concat([results, rlt_r])

    results.index = range(len(results))
    results.to_csv(FILE_NAME,encoding='utf-8')
'''

if __name__ == '__main__':
  main()

我第一次破坏它,它成功了。但第二次,有一些错误,看起来像是多线程。错误讯息:

Exception in thread Thread-3:
Traceback (most recent call last):
  File "/Users/mac/.edm/envs/edm/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/Users/mac/.edm/envs/edm/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/Users/mac/.edm/envs/edm/lib/python2.7/multiprocessing/pool.py", line 389, in _handle_results
    task = get()
  File "build/bdist.macosx-10.6-x86_64/egg/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
TypeError: ('__init__() takes at least 3 arguments (1 given)', <class 'googleapiclient.errors.HttpError'>, ())

我该如何解决?

0 个答案:

没有答案