have there was an API error : 403 : Insufficient Permission when upload cost data via google analytics api

4,361 views
Skip to first unread message

Stephen Hou

unread,
Aug 26, 2013, 4:41:55 AM8/26/13
to google-analytics...@googlegroups.com
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

Stephen Hou

unread,
Aug 26, 2013, 5:01:20 AM8/26/13
to google-analytics...@googlegroups.com
i also change the    scope='https://www.googleapis.com/auth/analytics.readonly', to scope='https://www.googleapis.com/auth/analytics', since i saw there is two differnce api file
and also i found there many error on the example of python file for upload the csv file, function name, and other paremeters

Pete Frisella

unread,
Aug 27, 2013, 10:35:11 AM8/27/13
to google-analytics...@googlegroups.com
1) Check to make sure that you have authorized for the scope: https://www.googleapis.com/auth/analytics
2) Check that the user you have authorized is an Admin user (at least Edit permissions) for that property.

also, can you list uploads (even if there are none to list), or does that also return a 403 permissions error?

Stephen Hou

unread,
Sep 2, 2013, 6:35:46 AM9/2/13
to google-analytics...@googlegroups.com
Hi Pete:
very glad to e-meet u here, actually i wrote to you already in august ,22th, with your gmail  address but no response :(

I have another question here, there is 3 types of client ID for API access, web application,installed application,service account,
because all the test i done is in my desktop, i dont have a hosting server  and dont not have domain to binding to the web application,
 i choose the  installed application API type, doese this affect the resutl, 
why i ask this questions because there is no email address for the installed application type api comparing to the web application and 
service account,  am i right to use the install application for this case(upload cost data)?
i saw some guide line to ask to add the email address which genearte from the web appllicatin or service account in Google api,
to add into the admin level of the GA, so i missed this step , doese step affect my results?

btw your question, 
1, i could open this page, but seems this page for my web browser ,it is only displayt analytics characters
2, i am the owner of the propoerlty ,i use the email address to login, i could use the same access to download report function to download my keyword data from the same , so i think i have the right access
   list upload method is successful in my sider see my screenshot

i have upload the two source code file ,please feel free to ask
1.jpg
2.jpg
analytics.rar

Stephen Hou

unread,
Sep 4, 2013, 1:37:14 PM9/4/13
to google-analytics...@googlegroups.com
have some one could help on my situation
thanks


On Monday, August 26, 2013 4:41:55 PM UTC+8, Stephen Hou wrote:
Reply all
Reply to author
Forward
0 new messages