英文:
Creating a file in Google Drive (G Suite) from Java with Service Account: 403 Insufficient Permission
问题
我正在尝试在Google Drive文件夹中创建一个文件。
从文件夹中读取是有效的。我已经在不同应用程序中使用不同服务账号读取文件。
我已将我的G Suite Drive文件夹与生成的服务账号电子邮件共享,并赋予了编辑者访问权限(读取、写入、编辑)。
我试图从我的Java Springboot应用程序复制一个现有文件(也可以从头开始创建一个空文件)。
public class TemplateDocumentManager {
@Value("${printing.template.id}")
private String baseTemplateFileId;
@Autowired
private Drive driveService;
public void createNewContractFromEmptyTemplate() throws IOException {
File templateFile = getDriveFiles().get(baseTemplateFileId).execute();
File newFileInstance = getDriveFiles()
.copy(baseTemplateFileId, templateFile)
.setSupportsAllDrives(true)
.execute();
log.error("Id of newly created file is: {}", newFileInstance.getId());
}
protected Drive.Files getDriveFiles() {
return driveService.files();
}
}
使用@Autowired
注解注入的Google Drive服务运行良好。它的创建方式如下:
@Configuration
public class DriveService {
public static final String APPLICATION_NAME = "appname";
@Autowired
GoogleCredential googleCredential;
@Bean("driveService")
public Drive createDriveService() throws GeneralSecurityException, IOException {
return new Drive.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), googleCredential)
.setApplicationName(APPLICATION_NAME)
.build();
}
...
}
关于服务账号需要什么权限才能在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.
public class TemplateDocumentManager {
@Value("${printing.template.id}")
private String baseTemplateFileId;
@Autowired
private Drive driveService;
public void createNewContractFromEmptyTemplate() throws IOException {
File templateFile = getDriveFiles().get(baseTemplateFileId).execute();
File newFileInstance = getDriveFiles()
.copy(baseTemplateFileId, templateFile)
.setSupportsAllDrives(true)
.execute();
log.error("Id of newly created file is: {}", newFileInstance.getId());
}
protected Drive.Files getDriveFiles() {
return driveService.files();
}
}
The google drive service injected with the @Autowired
annotation is working properly. It is created as follows:
@Configuration
public class DriveService {
public static final String APPLICATION_NAME = "appname";
@Autowired
GoogleCredential googleCredential;
@Bean("driveService")
public Drive createDriveService() throws GeneralSecurityException, IOException {
return new Drive.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), googleCredential)
.setApplicationName(APPLICATION_NAME)
.build();
}
...
}
Any ideas on what the service account needs to be able to write on the G Suite Drive folder?
答案1
得分: 1
你必须为GoogleCredentials对象授予所需的权限,即:
GoogleCredential.fromStream(source).createScoped(scopes)
作用域可以在SheetsScopes或DriveScopes中找到(取决于您的Google依赖项)。
英文:
You have to give the required permissions to the GoogleCredentials Object, i.e.
GoogleCredential.fromStream(source).createScoped(scopes)
Scopes could be found in e.g. SheetsScopes or DriveScopes ( depending on your Google dependencies )
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论