如何使用服务帐户在另一个用户文件夹中搜索 file/open/create googlesheet?

How do I search for a file/open/create a googlesheet in another users folder using a service account?

场景如下。我有一个帐户,用于保存测试结果电子表格。我有一个服务帐户,用于以编程方式(使用 java 和 google 驱动 apiv3)到 add/update 这些电子表格。我需要一种方法来创建电子表格并从服务帐户中搜索该用户帐户和文件夹中的电子表格。我有以下代码获取文件列表,但它是服务帐户文件夹中的文件,而不是用户帐户文件夹中的文件。有帮助吗?

private static boolean findSpreadsheetFile(String sSpreadsheetName)
                throws GeneralSecurityException, IOException, URISyntaxException
{
    String pageToken = null;
    // Build a new authorized API client service.
    Drive driveService = getDriveService();
    // Print the names and IDs
    do
    {
        FileList result = driveService.files().list()
                        .setPageToken(pageToken)
                        .setFields("nextPageToken, files(id, name)")
                        .execute();
        for (com.google.api.services.drive.model.File file : result.getFiles())
        {
            System.out.printf("Found file: %s (%s)\n", file.getName(), file.getId());
        }
        pageToken = result.getNextPageToken();
    }
    while (pageToken != null);
    return true;
}

public static Drive getDriveService() throws GeneralSecurityException, IOException, URISyntaxException
{
    Credential credential = authorizeDrive();
    return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
}

private static GoogleCredential authorizeDrive() throws GeneralSecurityException, IOException, URISyntaxException
{
    JacksonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    URL fileUrl = ParseTestResultsEmails.class.getResource(sP12Filename);
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
                    .setJsonFactory(JSON_FACTORY).setServiceAccountId(sServiceAccountID)
                    .setServiceAccountPrivateKeyFromP12File(new File(fileUrl.toURI()))
                    .setServiceAccountScopes(DRIVE_SCOPES).build();
    return credential;
}

基于此documentation,域管理员可以使用具有 OAuth 2.0 的服务帐户以这种方式委派权限。

In enterprise applications you may want to programmatically access users data without any manual authorization on their part. In G Suite domains, the domain administrator can grant to third party applications domain-wide access to its users' data — this is referred as domain-wide delegation of authority.

Warning: Service accounts should only be used for performing delegation where the effective identity is that of an individual user in a domain. Using the service account as a common owner to create many shared documents can have severe performance implications. Additionally, service accounts may not acquire additional storage quota, nor do they act as members of a domain.

您可以查看 Using OAuth 2.0 for Server to Server Applications 文档以获取更多信息。

似乎无法使用 API 在 Google 驱动器中执行我想要的操作。我最终做的是将 sheet 创建为 "Service Account",然后将所有权转让给我想要的用户,然后可以将其共享给需要访问权限的组。效果很好!