简体   繁体   English

带有GA Management API的上传数据脚本未定义“分析”错误

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

I'm trying to upload some custom data into GA with Python. 我正在尝试使用Python将一些自定义数据上传到GA中。 It's the first I'm doing this so I'm not sure about nothing. 这是我第一次这样做,所以我不确定。

I've build the following script based on the example from the doc . 我基于doc的示例构建了以下脚本。 When running it I have the following error : 运行它时,出现以下错误:

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

Here is my code : 这是我的代码:

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()

If my code isn't clear or show some other obvious mistake different from the one I'm questioning please be comprehensive I'm learning ! 如果我的代码不清楚,或者显示出与我正在询问的代码不同的其他明显错误,请全面学习!

Edit: I've checked in my API Manager the Analytics API is well enable 编辑:我已经在我的API管理器中检查了Analytics API是否已启用

Ok. 好。 It is a simple block alignement issue. 这是一个简单的块对齐问题。 I needed to align this part : 我需要对齐这部分:

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

with the first part ! 与第一部分!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM