我无法通过 Google Drive API v3 获取 Google 云端硬盘上文件的所有者。
我可以在 v2 下做到这一点,但事情已经改变了。
根据文档 https://developers.google.com/drive/api/v3/reference/permissions/get我需要:
- 列出文件的权限(没问题)
- 从该权限列表中查找具有“所有者”角色的权限的 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('[email protected] /cdn-cgi/l/email-protection')
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('[email protected] /cdn-cgi/l/email-protection')
f = service.files().get(fileId='1lASRBuAHRxEC-T0X5SdlF3w7X_168Q2QV9L0V6QaXUk').execute()
currentOwner = f['owners'][0]['emailAddress']