'analytics' 使用 GA 管理 API 上传数据脚本未定义错误

'analytics' is not defined error with Upload data script with GA Management API

我正在尝试使用 Python 将一些自定义数据上传到 GA。这是我第一次这样做,所以我什么都不确定。

我根据 doc 中的示例构建了以下脚本。当 运行 时出现以下错误:

  File "import.py", line 50, in <module>
    daily_upload = analytics.management().uploads().uploadData(
NameError: name 'analytics' is not defined

这是我的代码:

import argparse

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

import httplib2
import urllib2 
from oauth2client import client
from oauth2client import file
from oauth2client import tools



def get_service(api_name, api_version, scope, key_file_location,
                service_account_email):
  """Get a service that communicates to a Google API.

  Args:
    api_name: The name of the api to connect to.
    api_version: The api version to connect to.
    scope: A list auth scopes to authorize for the application.
    key_file_location: The path to a valid service account p12 key file.
    service_account_email: The service account email address.

  Returns:
    A service that is connected to the specified API.
  """

  credentials = ServiceAccountCredentials.from_p12_keyfile(
    service_account_email, key_file_location, scopes=scope)

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

  # Build the service object.
  service = build(api_name, api_version, http=http)

  return service

from apiclient.http import MediaFileUpload
try:
  media = MediaFileUpload('mycsv.csv',
                          mimetype='application/octet-stream',
                          resumable=False)
  daily_upload = analytics.management().uploads().uploadData(
      accountId='XXXXXX',
      webPropertyId='XXXXXXX',
      customDataSourceId='XXXXXXXXXX',
      media_body=media).execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print 'There was an error in constructing your query : %s' % error



def main():
  # Define the auth scopes to request.
  scope = ['https://www.googleapis.com/auth/analytics']

  # Use the developer console and replace the values with your
  # service account email and relative location of your key file.
  service_account_email = 'XXXXXX@XXXXXX'
  key_file_location = 'XXXXXXXXXX.p12'

  # Authenticate and construct service.
  service = get_service('analytics', 'v3', scope, key_file_location,
    service_account_email)
  profile = get_first_profile_id(service)
  print_results(get_results(service, profile))


if __name__ == '__main__':
  main()

如果我的代码不清晰或显示出与我正在质疑的代码不同的其他明显错误,请全面了解我正在学习!

编辑:我已经在我的 API 管理器中检查过 Analytics API 很好地启用了

好的。这是一个简单的块对齐问题。我需要对齐这部分:

from apiclient.http import MediaFileUpload
try:
  media = MediaFileUpload('mycsv.csv',
                          mimetype='application/octet-stream',
                          resumable=False)
  daily_upload = analytics.management().uploads().uploadData(
      accountId='XXXXXX',
      webPropertyId='XXXXXXX',
      customDataSourceId='XXXXXXXXXX',
      media_body=media).execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print 'There was an error in constructing your query : %s' % error

与第一部分!