使用API​​将图像上传到GDrive - Python

时间:2012-10-04 12:34:19

标签: python google-drive-api google-api-python-client

我想将aj图片上传到Google云端硬盘。该图像位于我上传图像所用脚本的同一目录中。 我在script.py文件中有这段代码

#!/usr/bin/python

import httplib2
import pprint
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow

filename = 'image.jpg'
mimetype = 'image/jpg'

drive_service = build('drive', 'v2', developerKey='MY Simple API Access KEY')

# Insert a file
media_body = MediaFileUpload(filename, mimetype=mimetype, resumable=True)
body = {
    'title': 'This is an image',
    'description': 'This is a description',
    'mimeType': mimetype
}

file = drive_service.files().insert(body=body, media_body=media_body).execute()

pprint.pprint(file)

运行 python script.py 时出现此错误:

Traceback (most recent call last):
  File "script.py", line 22, in <module>
    file = drive_service.files().insert(body=body, media_body=media_body).execute()
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 120, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/apiclient/http.py", line 656, in execute
    _, body = self.next_chunk(http=http)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 120, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/apiclient/http.py", line 733, in next_chunk
    raise ResumableUploadError("Failed to retrieve starting URI.")
apiclient.errors.ResumableUploadError: Failed to retrieve starting URI.

1 个答案:

答案 0 :(得分:1)

您未正确执行授权,请查看Google Drive SDK文档中的快速入门,了解分步说明:

https://developers.google.com/drive/quickstart