【发布时间】:2020-09-15 06:21:51
【问题描述】:
我有一个问题,找不到id文件夹,我想在我之前准备好的文件夹内的共享驱动器中创建文件,这是我的代码
public static void main (String[]args) throws GeneralSecurityException, IOException {
Drive service = CreateFolder.createService();
try {
String key = "1kM_fdJek3b4sErr_xxx";
File fileMetadata = new File();
fileMetadata.setName("invoice");
fileMetadata.setParents(Collections.singletonList(key));
fileMetadata.setMimeType("application/vnd.google-apps.folder");
File file = service.files().create(fileMetadata)
.setFields("id , parents").execute();
System.out.println("folder successfully created by id " + file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
private static Drive createService() throws GeneralSecurityException, IOException {
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("my-email-id")
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(new java.io.File("privateKey.p12"))
.build();
return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
}
这是文件夹的1kM_fdJek3b4sErr_xxx密钥,我正在使用服务帐户进行授权,这是我的私钥文件privateKey.p12。 我的电子邮件 ID 已被授予访问共享驱动器的权限(我的电子邮件 ID 是内容管理器)。但我遇到一个问题,即找不到文件夹 ID,如下面的消息
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"location" : "fileId",
"locationType" : "parameter",
"message" : "File not found: 1kM_fdJek3b4sErr_xxx.",
"reason" : "notFound"
} ],
"message" : "File not found: 1kM_fdJek3b4sErr_xxx."
}
我需要帮助才能在共享驱动器中包含的文件夹中创建文件
【问题讨论】:
标签: java google-api google-drive-api google-api-java-client