简体   繁体   中英

Uploading to Google Drive with Pythonanywhere

I'm a fairly new python user and I'm trying to upload a file to Google Drive using Pythonanywhere. I have tried my script (below) on my PC successfully, but when I try it on Pythonanywhere I get an error.

Portion of script:

from pydrive.drive import GoogleDrive

def sendtogoogle():
 drive = GoogleDrive()
 gpath = 'Myfolder'
 fname = 'Apicture.jpg'
 file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
 for file1 in file_list:
     if file1['title'] == gpath:
         id = file1['id']
 file1 = drive.CreateFile({'title': fname, "parents":  [{"kind": "drive#fileLink","id": id}]})
 file1.SetContentFile(fname)
 file1.Upload()

Error:

Traceback (most recent call last):
from pydrive.drive import GoogleDrive
File "/home/myusername/.local/lib/python2.7/site-packages/pydrive/drive.py", line 2, in <module>
from .files import GoogleDriveFile
File "/home/myusername/.local/lib/python2.7/site-packages/pydrive/files.py", line 4, in <module> from apiclient import errors
File "/usr/local/lib/python2.7/dist-packages/apiclient/__init__.py", line 18, in <module>
from googleapiclient import channel
File "/usr/local/lib/python2.7/dist-packages/googleapiclient/channel.py", line 63, in <module>
from googleapiclient import errors
File "/usr/local/lib/python2.7/dist-packages/googleapiclient/errors.py", line 26, in <module>
from oauth2client import util
ImportError: cannot import name util

Thanks in advance for any suggestions.

Based from issue #270 in GitHub, error encountered seems like the oauth2client compatibility issue that was fixed in 1.5.2.

You may want to try removing this line in your code if you haven't done so:

from oauth2client import util

For more information, you may also visit Google API Client Libraries > Python - Getting Started .

I had success using google-api-python-client on PythonAnywhere. I installed it with pip in a virtualenv.

My code was as follows.

    import httplib2
    import os
    from apiclient import discovery
    from googleapiclient.http import *
    from oauth2client import client
    from oauth2client import tools
    from oauth2client.file import Storage

    #This function was copied from the getting started guide on Google Drive API guide.
    credentials = get_credentials() 
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http)

    file_metadata = {'name' : how_you_want_name_of_file_to_appear_on_Google_Drive, 
                     'parents': [id_of_folder_in_Google_drive_to_upload_to]
                     }

     mime_string = 'application/pdf' #or whatever else you want
     media = MediaFileUpload(full_path_of_file_to_upload, mimetype = mime_string, resumable = False)

     file = service.files().create(body = file_metadata, media_body = media, fields = 'id').execute()

One caveat; I ran this on my local machine first and that's where I had the popup window about authorization. Then I just uploaded the credential file to my PythonAnywhere account. I never tried 'authorizing' from the PythonAnywhere server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM