下面的代码试图授予我先前使用类似代码和相同服务帐户模拟同一用户创建的共享驱动器的组fileOrganizer
权限。 (我已经用假冒的东西编辑了实际的DRIVE_ID
)
public class DriveQuickstart { public static void main(String... args) throws IOException, GeneralSecurityException { final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE); final String JSON_PATH = "my.json"; final String USER = "[email protected]"; final String GROUP = "[email protected]"; final String DRIVE_ID = "bbb"; GoogleCredential credential = GoogleCredential .fromStream(new FileInputStream(JSON_PATH)) .createScoped(SCOPES) .createDelegated(USER); Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build(); DriveList result = service.drives().list().execute(); System.out.println("drive list " + result); System.out.println("about to grant permission on id " + DRIVE_ID); service .permissions() .create(DRIVE_ID, new Permission() .setType("group") .setRole("fileOrganizer") .setEmailAddress(GROUP)) .execute(); } }
正如您从下面的输出中看到的,我的代码能够列出我现有的两个驱动器。第二个(ID编为
bbb
)是我要与该组共享的一个。当我在创建权限时使用相同的ID时,API会显示File not found
:
drive list {"drives":[{"id":"aaa","kind":"drive#drive","name":"Manually Created Shared Drive"}, {"id":"bbb","kind":"drive#drive","name":"Shared Drive Created By Service Account"}], "kind":"drive#driveList"} about to grant permission on id bbb Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found { "code" : 404, "errors" : [ { "domain" : "global", "location" : "fileId", "locationType" : "parameter", "message" : "File not found: bbb.", "reason" : "notFound" } ], "message" : "File not found: bbb." } at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:444) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1108) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:542) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:475) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:592) at DriveQuickstart.main(DriveQuickstart.java:63)
为什么说
File not found
?
我是否需要setSupportsAllDrives(true)
才能包含共享驱动器
该文档说,假设2020年6月1日之后,所有应用程序都支持共享驱动器。我正在使用截至今天(6月11日)的最新可能依赖项,但是所有这些依赖项都早于6月1日。这是我build.gradle文件中的依赖项部分:
dependencies {
compile 'com.google.api-client:google-api-client:1.30.9'
compile 'com.google.auth:google-auth-library-oauth2-http:0.20.0'
compile 'com.google.apis:google-api-services-drive:v3-rev20200413-1.30.9'
}
下面的代码试图授予组文件组织者对我之前使用相似代码和相同服务帐户模拟相同用户创建的共享驱动器的权限。 (我有...
想通了: