【问题标题】:Uploading to Google Drive with Pythonanywhere使用 Pythonanywhere 上传到 Google Drive
【发布时间】:2016-12-09 00:53:58
【问题描述】:

我是一个相当新的 python 用户,我正在尝试使用 Pythonanywhere 将文件上传到 Google Drive。我已经在我的 PC 上成功尝试了我的脚本(如下),但是当我在 Pythonanywhere 上尝试它时出现错误。

部分脚本:

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()

错误:

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

提前感谢您的任何建议。

【问题讨论】:

    标签: python upload google-drive-api pythonanywhere


    【解决方案1】:

    根据 GitHub 中的 issue #270,遇到的错误似乎是 1.5.2 中修复的 oauth2client 兼容性问题。

    如果您还没有这样做,您可能想尝试在代码中删除此行:

    from oauth2client import util
    

    更多信息,您也可以访问Google API Client Libraries > Python - Getting Started

    【讨论】:

      【解决方案2】:

      我在 PythonAnywhere 上成功使用了 google-api-python-client。我用 pip 在 vi​​rtualenv 中安装了它。

      我的代码如下。

          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()
      

      一个警告;我首先在我的本地机器上运行它,这就是我有关于授权的弹出窗口的地方。然后我刚刚将凭证文件上传到我的 PythonAnywhere 帐户。我从未尝试过从 PythonAnywhere 服务器“授权”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-03
        • 1970-01-01
        • 2012-11-09
        • 1970-01-01
        • 2020-03-10
        • 1970-01-01
        • 2016-10-06
        相关资源
        最近更新 更多