Frage

I want to upload aj image to Google Drive. The image is in the same directory of the script i did to upload the image. I have this code in a script.py file

#!/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)

when running python script.py I get this error:

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.
War es hilfreich?

Lösung

You are not performing authorization correctly, check the quickstart in the Google Drive SDK documentation for step-by-step instructions:

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top