通过python

时间:2018-05-29 05:29:49

标签: google-drive-api

我无法通过Google Drive API v3在Google云端硬盘上获取文件的所有者。

我可以在v2下做到这一点,但事情已经发生了变化。

根据我需要的documentation

  1. 列出文件的权限(没问题)
  2. 与所有者'查找权限的ID该权限列表中的角色(没问题)
  3. 获取该权限...应返回权限资源,其中应包含电子邮件地址(问题!)
  4. 不幸的是,我收到的内容包括部分信息,但不包括电子邮件地址。

    我怀疑我需要改变我的"得到"打电话告诉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']
    

1 个答案:

答案 0 :(得分:0)

太简单了...只需要在get调用中添加以下内容:

,字段=' EMAILADDRESS'

currentOwner = service.permissions().get(fileId='1lASRBuAHRxEC-T0X5SdlF3w7X_168Q2QV9L0V6QaXUk',permissionId='18137907375963748644',fields='emailAddress').execute()['emailAddress']