如何通过 Google Drive API v3 Java 使用服务帐户访问 Team Drive
How to access Team Drive using service account with Google Drive API v3 Java
下午好!
我无法访问 google 团队云端硬盘。
我需要创建文件夹并从本地存储上传文件,所有这些都应该由我的应用程序完成。
目前,我已经学会了连接到我的个人 Google 驱动器并在那里上传文件。我的个人存储连接代码:
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) //
.setApplicationName(APPLICATION_NAME).build();
// Print the names and IDs for up to 10 files.
FileList result = service.files().list().setPageSize(10).setFields("nextPageToken, files(id, name)").execute();
List<File> files = result.getFiles();
if (files == null || files.isEmpty()) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
System.out.printf("%s (%s)\n", file.getName(), file.getId());
}
}
告诉我,如何连接到 java 中的 Google Team Drive 并在那里上传文件?
谢谢:)
正在使用 java
中的服务帐户连接到团队云端硬盘
假设您已经满足先决条件,即
- 正在创建一个 Google 服务帐户
- 正在下载其凭据
- 要么与服务帐户共享团队驱动器,要么启用全域委派来模拟有权访问团队驱动器的用户
您的代码应如下所示:
private static final String APPLICATION_NAME = "YOUR APPLICATION";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final List < String > SCOPES = Collections.singletonList("XXXINSERTHEREYOURSCOPEXXXX");
public static void main(String...args) throws IOException, GeneralSecurityException {
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
File pk12 = new File("quickstartserv.p12");
String serviceAccount = "EMAIL FO YOUR SERVICE ACCOUNT.iam.gserviceaccount.com";
// Build service account credential.Builder necessary for the ability to refresh tokens
GoogleCredential getCredentials = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(serviceAccount)
.setServiceAccountPrivateKeyFromP12File(pk12)
.setServiceAccountScopes(SCOPES)
.setServiceAccountUser("xxx") //IF YOU WANT TO IMPERSONATE A USER
.build();
// Build a new authorized API client service.
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials)
.setApplicationName(APPLICATION_NAME)
.build();
FileList result = service.files().list().setPageSize(10).setQ('"ID OF THE SHARED DRIVE" in parents').setIncludeTeamDriveItems(true).setSupportsTeamDrives(true).setFields("nextPageToken, files(id, name)").execute();
...
}
请注意 setIncludeTeamDriveItems(true)
和 setSupportsTeamDrives(true)
是从共享驱动器检索文件所必需的。
下午好!
我无法访问 google 团队云端硬盘。
我需要创建文件夹并从本地存储上传文件,所有这些都应该由我的应用程序完成。
目前,我已经学会了连接到我的个人 Google 驱动器并在那里上传文件。我的个人存储连接代码:
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) //
.setApplicationName(APPLICATION_NAME).build();
// Print the names and IDs for up to 10 files.
FileList result = service.files().list().setPageSize(10).setFields("nextPageToken, files(id, name)").execute();
List<File> files = result.getFiles();
if (files == null || files.isEmpty()) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
System.out.printf("%s (%s)\n", file.getName(), file.getId());
}
}
告诉我,如何连接到 java 中的 Google Team Drive 并在那里上传文件? 谢谢:)
正在使用 java
中的服务帐户连接到团队云端硬盘假设您已经满足先决条件,即
- 正在创建一个 Google 服务帐户
- 正在下载其凭据
- 要么与服务帐户共享团队驱动器,要么启用全域委派来模拟有权访问团队驱动器的用户
您的代码应如下所示:
private static final String APPLICATION_NAME = "YOUR APPLICATION";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final List < String > SCOPES = Collections.singletonList("XXXINSERTHEREYOURSCOPEXXXX");
public static void main(String...args) throws IOException, GeneralSecurityException {
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
File pk12 = new File("quickstartserv.p12");
String serviceAccount = "EMAIL FO YOUR SERVICE ACCOUNT.iam.gserviceaccount.com";
// Build service account credential.Builder necessary for the ability to refresh tokens
GoogleCredential getCredentials = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(serviceAccount)
.setServiceAccountPrivateKeyFromP12File(pk12)
.setServiceAccountScopes(SCOPES)
.setServiceAccountUser("xxx") //IF YOU WANT TO IMPERSONATE A USER
.build();
// Build a new authorized API client service.
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials)
.setApplicationName(APPLICATION_NAME)
.build();
FileList result = service.files().list().setPageSize(10).setQ('"ID OF THE SHARED DRIVE" in parents').setIncludeTeamDriveItems(true).setSupportsTeamDrives(true).setFields("nextPageToken, files(id, name)").execute();
...
}
请注意 setIncludeTeamDriveItems(true)
和 setSupportsTeamDrives(true)
是从共享驱动器检索文件所必需的。