hi all:
i have encounted a problem when use the python to upload the meida data to google analytics via api,
i am successful to download the keywords report with current access but when i upload the cvs data , it is
showing i dont hae permit, but i am the account owner and i am sure the account id and web proer id is
correct so that is the problem
here is the code below
the accountid is the first part of web proper id
for example my tracking id is UA-123445-2
my account id is 123445 my web proper id is UA-123445-2
right
import sys
from apiclient.errors import HttpError
from oauth2client.client import AccessTokenRefreshError
from apiclient.http import MediaFileUpload
from apiclient.errors import HttpError
from apiclient import sample_tools
from oauth2client.client import AccessTokenRefreshError
import hello_analytics_api_v3_auth
import httplib2
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run
CLIENT_SECRETS = 'client_secrets.json'
MISSING_CLIENT_SECRETS_MESSAGE = '%s is missing' % CLIENT_SECRETS
FLOW = flow_from_clientsecrets(CLIENT_SECRETS,
message=MISSING_CLIENT_SECRETS_MESSAGE)
TOKEN_FILE_NAME = 'analytics.dat'
def prepare_credentials():
# Retrieve existing credendials
storage = Storage(TOKEN_FILE_NAME)
credentials = storage.get()
# If existing credentials are invalid and Run Auth flow
# the run method will store any new credentials
if credentials is None or credentials.invalid:
credentials = run(FLOW, storage) #run Auth Flow and store credentials
return credentials
def initialize_service():
# 1. Create an http object
http = httplib2.Http()
# 2. Authorize the http object
# In this tutorial we first try to retrieve stored credentials. If
# none are found then run the Auth Flow. This is handled by the
# prepare_credentials() function defined earlier in the tutorial
credentials = prepare_credentials()
http = credentials.authorize(http) # authorize the http object
# 3. Build the Analytics Service Object with the authorized http object
return build('analytics', 'v3', http=http)
service = initialize_service()
try:
media = MediaFileUpload('template4.csv', mimetype='application/octet-stream', resumable=False)
daily_upload = service.management().dailyUploads().upload(
accountId='65289xxx',
webPropertyId='UA-6528xxx-1',
customDataSourceId='dfsdfdsfdsfdsfsdfsdfsd,
date='2013-08-08',
appendNumber=1,
reset=0,
type='cost',
media_body=media).execute()
except TypeError, error:
# Handle errors in constructing a query.
print ('There was an error in constructing your query : %s' % error)
except HttpError, error:
# Handle API errors.
print ('Arg, there was an API error : %s : %s' %
(error.resp.status, error._get_reason()))
w