簡體   English   中英

帶有GA Management 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

與第一部分!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM