Google 驱动器 API 客户端 (Python):文件 ().insert() 的权限不足

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

我正在尝试让一个简单的 Python Google 驱动器上传器正常工作。我在开发人员控制台中创建了一个项目,启用了 Drive API 并添加了一个 OAuth 2.0 客户端 ID(其他应用程序类型)。

我可以在 Google Drive 的设置 -> 管理应用程序中看到该应用程序,并且可以从 [=30] 成功执行 Python Drive API 客户端提供的许多操作=]. files ().insert () 但是失败了:

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">

这是为了插入到我已设置为对所有人都可写的目录中,如下所示:

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

其中列出的权限为:

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

任何人都可以指出我缺少的权限吗?

根据@DalmTo 的建议,问题是 Auth 范围不正确:

 flow_from_clientsecrets()

Files: insert 需要至少以下范围之一的授权

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

检查您正在使用哪个范围进行身份验证。

如果修改这些范围或权限,请尝试删除您之前保存的与凭据相关的文件或浏览器缓存(如果有)。