如何修复“超时:_ssl.c:1039:握手操作超时”

时间:2019-07-31 09:38:00

标签: python google-analytics-api

我正在尝试使用Google Analytics(分析)API,但出现超时错误。我不知道如何解决此问题。

def initialize_analyticsreporting():

    credentials = ServiceAccountCredentials.from_json_keyfile_name(
    KEY_FILE_LOCATION, SCOPES)

    analytics = build('analyticsreporting', 'v4',credentials=credentials)

    return analytics

def get_report(analytics):

    return analytics.reports().batchGet(
  body={
    'reportRequests': [
    {
      'viewId': VIEW_ID,
      'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
      'metrics': [{'expression': 'ga:sessions'}],
      'dimensions': [{'name': 'ga:country'}]
    }]
  }).execute()

def print_response(response):

   for report in response.get('reports', []):
    columnHeader = report.get('columnHeader', {})
    dimensionHeaders = columnHeader.get('dimensions', [])
    metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])

    for row in report.get('data', {}).get('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):
                print('Date range: ' + str(i))
                for metricHeader, value in zip(metricHeaders, values.get('values')):
                    print(metricHeader.get('name') + ': ' + value)


def main():
    analytics = initialize_analyticsreporting()
    response = get_report(analytics)
    print_response(response)

if __name__ == '__main__':
    main()

我希望输出是包含我请求的数据的数据帧,但我收到错误消息“超时:_ssl.c:1039:握手操作超时”

0 个答案:

没有答案