java-如何使用服务帐户凭据将文件上传到Google Team Drive中的文件夹? - 堆栈溢出
我正在尝试使用Google Drive Java API v3为我们的应用程序实施一项新服务,该服务负责将文件上传到Google Team Drive中的特定文件夹。我使用公司专门为此项目创建的服务帐户,并且还从Google Developer Console生成了一个包含私钥的JSON文件。我还使用电子邮件xxxxx@xxxx.iam.gserviceaccount.com将文件夹共享到服务帐户,并将Content Manager的权限授予了共享的Team Drive。此外,由于某些原因,未授予该服务帐户整个G Suite范围的权限。
我要在这里实现的目标是:我想使用服务帐户的生成的私钥构建并返回授权的Google Drive客户服务,因此能够发送将文件上传到Google Team Drive中的文件夹的请求。
我目前使用的是:
- IntelliJ IDEA IntelliJ IDEA 2018.1.7(最终版)
- 春季靴2
- Java 10.0.1
问题是什么:
我无法成功返回授权的Google云端硬盘客户端服务,并且根本没有发送上传文件的请求。更令人困惑的是,没有引发异常。但是,将成功返回带有访问令牌和到期时间的凭据。
我已经阅读/找到的内容:
使用OAuth2.0从服务器到服务器的应用程序:https : //developers.google.com/identity/protocols/OAuth2ServiceAccount
创建对Drive API的简单请求的Java快速入门:https: //developers.google.com/drive/api/v3/quickstart/java
云端硬盘API的JavaDoc参考:https : //developers.google.com/resources/api-libraries/documentation/drive/v3/java/latest/
如何使用服务帐户凭据将文件上传到Google驱动器:如何使用服务帐户凭据 将文件上传到Google驱动器
如何使用带有Google Drive .NET API v3的服务帐户访问Team Drive: 如何使用带有Google Drive .NET API v3的服务帐户访问Team Drive
验证使用Java的Google驱动器API客户端库上传驱动器中的文件验证使用 Java的Google驱动器API客户端库 上传驱动器中的文件
我已经尝试过的:
- 使用GoogleCredential-class返回凭据(该类似乎已弃用:https: //googleapis.dev/java/google-api-client/latest/ )
- 通过查看Github项目和教程来查找不推荐使用的类的替代品
- 更新并检查pom.xml中所有缺少的依赖项
- 检查了共享Team Drive的设置,以确保已与服务帐户共享
- 添加了日志以确保问题确实是我上面描述的
- 尝试使用Java快速入门创建对Drive API教程的简单请求(但是,事实证明这并不完全适合我们项目的需求)
ContractStateUpdateService.java的相关部分:
File fileMetadata = new File();
fileMetadata.setName(fileTitle);
// setting the id of folder to which the file must be inserted to
fileMetadata.setParents(Collections.singletonList("dumbFolderId"));
fileMetadata.setMimeType("application/pdf");
byte[] pdfBytes = Base64.getDecoder().decode(base64File.getBytes(StandardCharsets.UTF_8));
InputStream inputStream = new ByteArrayInputStream(pdfBytes);
// decoding base64 to PDF and its contents to a byte array without saving the file on the file system
InputStreamContent mediaContent = new InputStreamContent("application/pdf", inputStream);
logger.info("Starting to send the request to drive api");
File file = DriveUtils.getDriveService().files().create(fileMetadata, mediaContent).execute();
logger.info("Succesfully uploaded file: " + file.getDriveId());
DriveUtils.java:
public class DriveUtils {
private static final String APPLICATION_NAME = "Google Drive Service";
// setting the Drive scope since it is essential to access Team Drive
private static List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE);
// private key is stored at the root of the project for now
private static String PRIVATE_KEY_PATH = "/path/to/private_key.json";
private static final Logger logger = LoggerFactory.getLogger(DriveUtils.class);
// build and return an authorized drive client service
public static Drive getDriveService() throws IOException, GeneralSecurityException {
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredentials credentials;
try (FileInputStream inputStream = new FileInputStream(PRIVATE_KEY_PATH)){
credentials = ServiceAccountCredentials.fromStream(inputStream).createScoped(SCOPES);
credentials.refreshIfExpired();
AccessToken token = credentials.getAccessToken();
logger.info("credentials: " + token.getTokenValue());
} catch (FileNotFoundException ex) {
logger.error("File not found: {}", PRIVATE_KEY_PATH);
throw new FileNotFoundException("File not found: " + ex.getMessage());
}
logger.info("Instantiating client next");
// Instantiating a client: this is where the client should be built but nothing happens... no exceptions!
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, (HttpRequestInitializer) credentials)
.setApplicationName(APPLICATION_NAME)
.build();
// this log should appear immediately after the client has been instantiated but still nothing happens
logger.info("Client instantiated");
return service;
}
}
pom.xml:
<!-- https://mvnrepository.com/artifact/com.google.api-client/google-api-client -->
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.29.2</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-drive</artifactId>
<version>v3-rev165-1.25.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.auth/google-auth-library-oauth2-http -->
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.16.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2 -->
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.oauth-client/google-oauth-client-jetty -->
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.29.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
我确定我在这里遗漏了一些东西,我事先为我的英语道歉。任何帮助将不胜感激。
-
1个
-
我在使用它们时发生了一件这样的事情
TranslationClient
,那是因为我的系统时间未同步,并且令牌甚至在使用前就过期了。尽管这只是我的经验,但这也可能是您的原因。 – Navjot Singh 6月14日13:24
感谢您的评论,这里的建议很有帮助,值得研究。但是,我将在此处介绍的解决方案无法直接回答有关我的代码如何或为什么不产生任何错误消息的问题。因此,现在,这是我针对该问题的解决方法:
- 启用云端硬盘API。在阅读了有关从服务帐户向Drive API发出请求的文章和文档的大部分内容之后,很明显,如果我们没有从Google API Console中启用Drive API,我的代码将无法正常工作。
- 将三个依赖项的版本降级到1.23.0。
pom.xml:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-drive</artifactId>
<version>v3-rev110-1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.23.0</version>
</dependency>
- 将属性的值设置
setSupportsTeamDrive
为true。没有该属性,我们将根本无法将文件保存到Team Drive中的共享文件夹中。
ContractStateUpdateService.java:
File fileMetadata = new File();
fileMetadata.setName(fileTitle);
// setting the id of folder to which the file must be inserted to
fileMetadata.setParents(Collections.singletonList("dumbTeamDriveId"));
fileMetadata.setMimeType("application/pdf");
// decoding base64 to PDF and its contents to a byte array without saving the file on the file system
byte[] pdfBytes = Base64.getDecoder().decode(base64File.getBytes(StandardCharsets.UTF_8);
InputStream inputStream = new ByteArrayInputStream(pdfBytes);
InputStreamContent mediaContent = new InputStreamContent("application/pdf", inputStream);
try {
// upload updated agreement as a PDF file to the Team Drive folder
DriveUtils.getDriveService().files().create(fileMetadata, mediaContent)
.setSupportsTeamDrives(true) // remember to set this property to true!
.execute();
} catch (IOException ex) {
logger.error("Exception: {}", ex.getMessage());
throw new IOException("Exception: " + ex.getMessage());
} catch (GeneralSecurityException ex) {
logger.error("Exception: {}", ex.getMessage());
throw new GeneralSecurityException("Exception: " + ex.getMessage());
}
- 拆分方法以使逻辑更清晰。
更新了DriveUtils-class中的代码:
// create and return credential
private static Credential getCredentials() throws IOException {
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(PRIVATE_KEY_PATH))
.createScoped(SCOPES);
return credential;
}
// build and return an authorized drive client service
public static Drive getDriveService() throws IOException, GeneralSecurityException {
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
// Instantiating a client
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials())
.setApplicationName(APPLICATION_NAME)
.build();
return service;
}
你的答案
以访客身份发布
必需,但从未显示
不是您要找的答案?浏览标记为java spring-boot google-drive-api google-api-java-client 服务帐户的其他问题, 或者询问您自己的问题。
Tags:
November 20, 2019 at 09:41AM
Open in Evernote
评论
发表评论