【问题标题】:Using Google Admin to view Drive files Domain-wide使用 Google Admin 查看域范围内的云端硬盘文件
【发布时间】:2017-08-21 20:55:48
【问题描述】:

我正在尝试列出域范围内的所有 Google 云端硬盘文件,包括仍在此处工作的用户和已继续工作的用户。有了它,我们可以 grep 某些术语(以前的客户)的输出以删除与客户相关的文件。

我相信我有一个成功的方法来列出所有使用 Admin SDK Quickstart 的用户,因为我们总共只有大约 200 个用户(最多 500 个)。我还有一种方法可以使用 Drive REST API 的 files.list() 方法为用户列出所有文件。 我需要知道的是如何迭代地模拟每个用户,以便运行文件列表脚本。

我找到了简介 .setServiceAccountUser(someone@domain.com),但我不确定在哪里实施,无论是在服务帐户授权步骤中,还是在单独的中间人脚本中。

【问题讨论】:

    标签: python google-drive-api google-admin-sdk


    【解决方案1】:

    您需要实现authorization flow for Service Accounts

    在 GCP 项目 (console.developers.google.com) 中创建服务帐号后,启用 DWD(域范围委派),然后在 G Suite 管理控制台中授权该服务帐号,即可使用该密钥“冒充” G Suite 实例中的任何帐户:

    从 json 文件创建凭证对象

    from oauth2client.service_account import ServiceAccountCredentials
    scopes = ['https://www.googleapis.com/auth/gmail.readonly']
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
        '/path/to/keyfile.json', scopes=scopes)
    

    创建一个可以模拟 user@example.org 的凭据(尽管可以是域中的任何用户)

    delegated_credentials = credentials.create_delegated('user@example.org')
    

    授权凭证对象(即获取 access_token)

    from httplib2 import Http
    http_auth = credentials.authorize(Http())
    

    调用 Gmail API:

    from apiclient import discovery
    service = discovery.build('gmail', 'v1', http=http)
    response = service.users().messages().list(userId='user@example.org').execute()
    

    【讨论】:

      【解决方案2】:

      看看https://github.com/pinoyyid/googleDriveTransferOwnership/blob/master/src/couk/cleverthinking/tof/Main.java

      特别是第 285-299 行,处理为模拟用户生成凭据。

         GoogleCredential.Builder builder = new GoogleCredential.Builder()
                      .setTransport(HTTP_TRANSPORT)
                      .setJsonFactory(JSON_FACTORY)
                      .setServiceAccountId(serviceAccountEmailAddress)
                      .setServiceAccountPrivateKeyFromP12File(f)
                      .setServiceAccountScopes(Collections.singleton(SCOPE));
              // if requested, impresonate a domain user
              if (!"ServiceAccount".equals(impersonatedAccountEmailAddress)) {
                  builder.setServiceAccountUser(impersonatedAccountEmailAddress);
              }
      
              // build the Drive service
              Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, null)
                      .setApplicationName("TOF")
                      .setHttpRequestInitializer(builder.build()).build();
      

      这是 Java,但至少应该告诉你步骤是什么。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-28
        • 2022-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多