“Analytics(分析)”未定义与GA管理API上传数据脚本的错误
问题描述:
我试图用Python将一些自定义数据上传到GA中。这是我第一次这样做,所以我不知道什么。“Analytics(分析)”未定义与GA管理API上传数据脚本的错误
我已经根据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 = '[email protected]'
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
第一部分!
在该文档中有“此代码假定您拥有授权的Google Analytics服务对象。” – ettanany
我在https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/service-py下面的文档中完成了这个工作,这部分工作正常。我想我不明白,但我不知道是什么。 –