简体   繁体   中英

'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. 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 . 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

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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