在Google Drive(G Suite)中使用服务帐号从Java创建文件:403权限不足

huangapple go评论98阅读模式
英文:

Creating a file in Google Drive (G Suite) from Java with Service Account: 403 Insufficient Permission

问题

我正在尝试在Google Drive文件夹中创建一个文件。

从文件夹中读取是有效的。我已经在不同应用程序中使用不同服务账号读取文件。

我已将我的G Suite Drive文件夹与生成的服务账号电子邮件共享,并赋予了编辑者访问权限(读取、写入、编辑)。

我试图从我的Java Springboot应用程序复制一个现有文件(也可以从头开始创建一个空文件)。

  1. public class TemplateDocumentManager {
  2. @Value("${printing.template.id}")
  3. private String baseTemplateFileId;
  4. @Autowired
  5. private Drive driveService;
  6. public void createNewContractFromEmptyTemplate() throws IOException {
  7. File templateFile = getDriveFiles().get(baseTemplateFileId).execute();
  8. File newFileInstance = getDriveFiles()
  9. .copy(baseTemplateFileId, templateFile)
  10. .setSupportsAllDrives(true)
  11. .execute();
  12. log.error("Id of newly created file is: {}", newFileInstance.getId());
  13. }
  14. protected Drive.Files getDriveFiles() {
  15. return driveService.files();
  16. }
  17. }

使用@Autowired注解注入的Google Drive服务运行良好。它的创建方式如下:

  1. @Configuration
  2. public class DriveService {
  3. public static final String APPLICATION_NAME = "appname";
  4. @Autowired
  5. GoogleCredential googleCredential;
  6. @Bean("driveService")
  7. public Drive createDriveService() throws GeneralSecurityException, IOException {
  8. return new Drive.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), googleCredential)
  9. .setApplicationName(APPLICATION_NAME)
  10. .build();
  11. }
  12. ...
  13. }

关于服务账号需要什么权限才能在G Suite Drive文件夹上进行写入,有什么想法吗?

英文:

I'm trying to create a file in a Google Drive folder.

Reading from the folder works. I'm already reading files in different applications with different Service Accounts.

I shared my G Suite Drive folder with the generated service account email and gave it Editor access (read, write, edit).

I tried to copy an existing file (but also create an empty one from scratch) from my Java Springboot application.

  1. public class TemplateDocumentManager {
  2. @Value("${printing.template.id}")
  3. private String baseTemplateFileId;
  4. @Autowired
  5. private Drive driveService;
  6. public void createNewContractFromEmptyTemplate() throws IOException {
  7. File templateFile = getDriveFiles().get(baseTemplateFileId).execute();
  8. File newFileInstance = getDriveFiles()
  9. .copy(baseTemplateFileId, templateFile)
  10. .setSupportsAllDrives(true)
  11. .execute();
  12. log.error("Id of newly created file is: {}", newFileInstance.getId());
  13. }
  14. protected Drive.Files getDriveFiles() {
  15. return driveService.files();
  16. }
  17. }

The google drive service injected with the @Autowired annotation is working properly. It is created as follows:

  1. @Configuration
  2. public class DriveService {
  3. public static final String APPLICATION_NAME = "appname";
  4. @Autowired
  5. GoogleCredential googleCredential;
  6. @Bean("driveService")
  7. public Drive createDriveService() throws GeneralSecurityException, IOException {
  8. return new Drive.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), googleCredential)
  9. .setApplicationName(APPLICATION_NAME)
  10. .build();
  11. }
  12. ...
  13. }

Any ideas on what the service account needs to be able to write on the G Suite Drive folder?

答案1

得分: 1

你必须为GoogleCredentials对象授予所需的权限,即:

  1. GoogleCredential.fromStream(source).createScoped(scopes)

作用域可以在SheetsScopes或DriveScopes中找到(取决于您的Google依赖项)。

英文:

You have to give the required permissions to the GoogleCredentials Object, i.e.

  1. GoogleCredential.fromStream(source).createScoped(scopes)

Scopes could be found in e.g. SheetsScopes or DriveScopes ( depending on your Google dependencies )

huangapple
  • 本文由 发表于 2020年10月5日 04:35:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64199628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定