Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive API Client (Python): Insufficient Permission for files().insert()

I am trying to get a simple Python Google Drive uploader working. I've created a project in the developer console, enabled the Drive API and added an OAuth 2.0 client ID (application type Other).

I can see the application listed in the Google Drive's Settings -> Manage Apps, and can successfully execute many operations provided by the Python Drive API client from Google. files ().insert () however fails with:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&convert=false&useContentAsIndexableText=false&alt=json returned "Insufficient Permission">

This is for an insert into a directory which I have made writable to everyone, as seen below:

credentials = get_credentials ()
http = credentials.authorize (httplib2.Http ())
service = discovery.build ('drive', 'v2', http=http)

PARENT_ID="0B1gLgXwTiUzlfmo0UGVsZ1NWdW1nZG9OcENNYkJua2E1d0pqWE14TjFyc2hVMHdEU1h5czQ"

perms = service.permissions().list(fileId=PARENT_ID).execute()

print ("PERMISSIONS:")
for perm in perms["items"]:
    for p in perm:
        print (p, perm[p])

print

parent = {
    "isRoot": False,
    "kind": "drive#parentReference",
    "id": PARENT_ID
}

service.files ().insert (
    body = {"parents" : [parent]},
    media_body='./test.txt',
    convert=False,
    useContentAsIndexableText=False
).execute ()

Which lists the permission as:

(u'withLink', True)
(u'kind', u'drive#permission')
(u'etag', u'"F-w0rsCIWtQP8RGyv_V1DlKfcRk/icwHkDdfUYuMzqZrUsVIyvu85K8"')
(u'role', u'writer')
(u'type', u'anyone')
(u'id', u'anyoneWithLink')
(u'selfLink', u'https://www.googleapis.com/drive/v2/files/0B1gLgXwTiUzlfmo0UGVsZ1NWdW1nZG9OcENNYkJua2E1d0pqWE14TjFyc2hVMHdEU1h5czQ/permissions/anyoneWithLink')

Can anyone point me to which permission I am missing, please?

like image 895
Gwen Ives Avatar asked Aug 31 '15 10:08

Gwen Ives


People also ask

How do I create a Google Drive folder in Google Drive API?

To create a folder, use the files. create method with the application/vnd. google-apps. folder MIME type and a title.


3 Answers

Files: insert requires authorization with at least one of the following scopes

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.appdata
https://www.googleapis.com/auth/drive.apps.readonly

Check which scope you are authenticating with.

like image 180
DaImTo Avatar answered Oct 20 '22 18:10

DaImTo


If modifying these scopes or permissions, try deleting your previously saved credentials related file or browser cache if any.

like image 41
Md Tahmilur Rahman Avatar answered Oct 20 '22 17:10

Md Tahmilur Rahman


As advised by @DalmTo, the issue was an incorrect Auth scope in:

 flow_from_clientsecrets()
like image 27
Gwen Ives Avatar answered Oct 20 '22 19:10

Gwen Ives