【发布时间】:2018-05-29 05:29:49
【问题描述】:
我无法通过 Google Drive API v3 获取 Google Drive 上文件的所有者。
我可以在 v2 下执行此操作,但情况发生了变化。
根据documentation我需要:
- 列出文件的权限(没问题)
- 从该权限列表中查找具有“所有者”角色的权限 ID(没问题)
- 获取该权限...应该返回一个权限资源,其中应该包括电子邮件地址(问题!)
很遗憾,我返回的内容包括一些信息,但不包括电子邮件地址。
我怀疑我需要更改我的“get”调用来告诉 API 我要查找哪些字段,但我不知道该怎么做。
这就是我所拥有的(v3):
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient.discovery import build
def build_service(user):
keyfile = 'C:\Python27\Scripts\Certificates for Transfer owner script\Transfer Ownership on Drive-f240cff252af.json'
SCOPE = 'https://www.googleapis.com/auth/drive'
credentials = ServiceAccountCredentials.from_json_keyfile_name(keyfile, scopes=SCOPE).create_delegated(user)
http_auth = credentials.authorize(Http())
return build('drive', 'v2', http=http_auth)
service = build_service('lpglobaldrive@lonelyplanet.com.au')
f = service.files().get(fileId='1lASRBuAHRxEC-T0X5SdlF3w7X_168Q2QV9L0V6QaXUk').execute()
p = service.permissions().get(fileId='1lASRBuAHRxEC-T0X5SdlF3w7X_168Q2QV9L0V6QaXUk',permissionId='18137907375963748644').execute()
currentOwner = p['emailAddress']
不幸的是,我收到“KeyError: 'emailAddress'”(如果我查看“p”的内容,有角色、种类、类型和 id,但没有电子邮件地址)。
这对我有用(使用 v2):
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient.discovery import build
def build_service(user):
keyfile = 'C:\Python27\Scripts\Certificates for Transfer owner script\Transfer Ownership on Drive-f240cff252af.json'
SCOPE = 'https://www.googleapis.com/auth/drive'
credentials = ServiceAccountCredentials.from_json_keyfile_name(keyfile, scopes=SCOPE).create_delegated(user)
http_auth = credentials.authorize(Http())
return build('drive', 'v2', http=http_auth)
service = build_service('lpglobaldrive@lonelyplanet.com.au')
f = service.files().get(fileId='1lASRBuAHRxEC-T0X5SdlF3w7X_168Q2QV9L0V6QaXUk').execute()
currentOwner = f['owners'][0]['emailAddress']
【问题讨论】:
-
看来Drive API v3的脚本有
return build('drive', 'v2', http=http_auth)。 -
糟糕...剪切和粘贴错误(在我的脚本中是“v3”)。
-
将 fields=* 添加到您的请求应返回所有元数据。
-
谢谢...很高兴知道!
标签: google-drive-api