Error 403 when trying to upload file to google drive via python -


so i'm new python (and first post stack overflow). i'm trying use python upload , download files , google drive account (and reference files drive on customized work tiki wiki). code below google's python api resources. list files on drive (as should). however, when attempt upload file (see 10th line bottom) given following error:

an error occured: <httperror 403 "insufficient permission"> 

i've been looking around few hours , can't figure out how around this. i'm thinking need request sort of token. not sure. again, i'm new this. appreciated!

import httplib2 import os  apiclient import discovery import oauth2client oauth2client import client oauth2client import tools  try:     import argparse     flags = argparse.argumentparser(parents=[tools.argparser]).parse_args() except importerror:     flags = none  scopes = 'https://www.googleapis.com/auth/drive.metadata.readonly' client_secret_file = 'client_secret.json' application_name = 'drive api quickstart'   def get_credentials():     """gets valid user credentials storage.      if nothing has been stored, or if stored credentials invalid,     oauth2 flow completed obtain new credentials.      returns:         credentials, obtained credential.     """     home_dir = os.path.expanduser('~')     credential_dir = os.path.join(home_dir, '.credentials')     if not os.path.exists(credential_dir):         os.makedirs(credential_dir)     credential_path = os.path.join(credential_dir,                                    'drive-quickstart.json')      store = oauth2client.file.storage(credential_path)     credentials = store.get()     if not credentials or credentials.invalid:         flow = client.flow_from_clientsecrets(client_secret_file, scopes)         flow.user_agent = application_name         if flags:             credentials = tools.run_flow(flow, store, flags)         else: # needed compatability python 2.6             credentials = tools.run(flow, store)         print 'storing credentials ' + credential_path     return credentials  apiclient import errors apiclient.http import mediafileupload # ...  def insert_file(service, title, description, parent_id, mime_type, filename):   """insert new file.    args:     service: drive api service instance.     title: title of file insert, including extension.     description: description of file insert.     parent_id: parent folder's id.     mime_type: mime type of file insert.     filename: filename of file insert.   returns:     inserted file metadata if successful, none otherwise.   """   media_body = mediafileupload(filename, mimetype=mime_type, resumable=true)   body = {     'title': title,     'description': description,     'mimetype': mime_type   }   # set parent folder.   if parent_id:     body['parents'] = [{'id': parent_id}]    try:     file = service.files().insert(         body=body,         media_body=media_body).execute()      # uncomment following line print file id     # print 'file id: %s' % file['id']      return file   except errors.httperror, error:     print 'an error occured: %s' % error     return none  def main():     """shows basic usage of google drive api.      creates google drive api service object , outputs names , ids     10 files.     """     credentials = get_credentials()     http = credentials.authorize(httplib2.http())     service = discovery.build('drive', 'v2', http=http)      insert_file(service, 'picture.jpg', 'no_description', false, 'image/jpeg', '/users/ethankay/documents/work/current_work/astrophysics/code/logger_program/master/testuploadfiles/test3.jpg')      results = service.files().list(maxresults=10).execute()     items = results.get('items', [])     if not items:         print 'no files found.'     else:         print 'files:'         item in items:             print '{0} ({1})'.format(item['title'], item['id'])     if __name__ == '__main__':     main() 

your google drive scope

scopes = 'https://www.googleapis.com/auth/drive.metadata.readonly'

which has no permission upload files. need change

scopes = 'https://www.googleapis.com/auth/drive'

to able manage files. try re-authenticate google api project.


Comments