Java谷歌Api[服务帐户]
我无法将文件上载到特定的驱动器文件夹 我的Java代码是:Java谷歌Api[服务帐户],java,google-drive-api,google-drive-android-api,Java,Google Drive Api,Google Drive Android Api,我无法将文件上载到特定的驱动器文件夹 我的Java代码是: HttpTransport httpTransport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); try { GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJso
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
try {
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(IRingeeConstants.SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton(DriveScopes.DRIVE_APPDATA)).setServiceAccountPrivateKeyFromP12File(new java.io.File("G:\\Ringee-1a1f1b786226.p12")).build();
Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).setApplicationName(IRingeeConstants.APPLICATION_NAME).build();
File body = new File();
body.setTitle("ringee");
body.setDescription("ringeeapp");
body.setMimeType("application/vnd.google-apps.folder");
java.io.File fileContent = new java.io.File("G:\\document.txt");
FileContent mediaContent = new FileContent("text/plain", fileContent);
File file = service.files().insert(body, mediaContent).execute();
System.out.print("file id is :" + file.getId());
Permission newPermission = new Permission();
// for showing files in browser that reason only using additional
// permission
newPermission.setValue(IRingeeConstants.USER_ACCOUNT_EMAIL);
newPermission.setType("owner");
newPermission.setRole("writer");
service.permissions().insert(file.getId(), newPermission).execute();
getFileByFileId(service, file.getId());
} catch (Exception e) {
e.printStackTrace();
}
文件上载到根文件夹中。为什么会这样
如何解决此问题。在Google drive中使用的是文件夹名
默认情况下,文件放置在根目录中。如果你在底部运行或尝试,你会得到一个谷歌硬盘上所有文件的列表
mimetype为application/vnd.google apps.folder
的文件是目录
{
"kind": "drive#file",
"id": "0B5pJkOVaKccEfjVaajNvRFNTa3pRZ2NlUmFWTjczaGpjaHE1NFo5bWFBWTJPOGU2TGtOTzA",
"title": "AppScripts",
"mimeType": "application/vnd.google-apps.folder",
"parents": [
{
"kind": "drive#parentReference",
"id": "0AJpJkOVaKccEUk9PVA",
"parentLink": "https://www.googleapis.com/drive/v2/files/0AJpJkOVaKccEUk9PVA",
"isRoot": true
}
在上面,您可以看到我的谷歌硬盘上的AppScripts
目录。如果您在parents下看到isRoot
,您可以知道它在根目录中
{
"kind": "drive#file",
"id": "0B1bbSFgVLpoXcEhfVDRFRF8tTkU",
"title": "GDE-app-team",
"mimeType": "application/vnd.google-apps.folder",
"parents": [
{
"kind": "drive#parentReference",
"id": "0B_UBp7FcUna-NU5abkhxYWVocTA",
"selfLink": "https://www.googleapis.com/drive/v2/files/0B1bbSFgVLpoXcEhfVDRFRF8tTkU/parents/0B_UBp7FcUna-NU5abkhxYWVocTA",
"parentLink": "https://www.googleapis.com/drive/v2/files/0B_UBp7FcUna-NU5abkhxYWVocTA",
"isRoot": false
}
在上面,您可以看到我的GDE app team
目录,如果您注意到它的isRoot设置为false,那么它不在根目录中。它所在的目录是0B_UBp7FcUna-NU5abkhxYWVocTA
我必须执行一个文件。使用该id查找GDE应用程序团队所在的目录名
"kind": "drive#file",
"id": "0B_UBp7FcUna-NU5abkhxYWVocTA",
"etag": "\"WsLEI6l9KW9DlzjU9lm9xLuMVm8/MTQwOTkyOTg4Njc0Mw\"",
"selfLink": "https://www.googleapis.com/drive/v2/files/0B_UBp7FcUna-NU5abkhxYWVocTA",
"alternateLink": "https://docs.google.com/folderview?id=0B_UBp7FcUna-NU5abkhxYWVocTA&usp=drivesdk",
"iconLink": "https://ssl.gstatic.com/docs/doclist/images/icon_11_shared_collection_list_1.png",
"title": "GDE",
您需要将父id添加到主体中
parentId可选父文件夹的ID
我不是java专家,但从文档中可以看出:
body.setParents(Arrays.asList(new ParentReference().setId(parentId)));
我通常使用文件列表来搜索所有目录
mimeType = 'application/vnd.google-apps.folder'
这将返回需要添加到父目录中的文件ID。下面是一个get命令,用于在父目录中创建文件。父目录ID可以通过名称、mime、
我指的是,对于其余部分(您正在使用的部分)和gdaaapi,都有一个完整的CRUD包装器
不过要小心,在Google Drive中,文件/文件夹名只是一个元数据字段。i、 e您可能在同一位置有多个同名的文件夹/文件(文件夹)。只有ID(有时称为资源ID)是唯一的。这意味着,如果您有一个文件夹名并执行搜索,最好检查您是否只有一个对象-正确的对象(检查其父id、mime类型等)。基本上,你的全部逻辑必须考虑到这一点
希望有帮助,祝你好运。回答有点晚,但下面是一个简单的方法,只要你有可用的凭据,就可以将文件上传到Google Drive。获取授权的逻辑可以写入另一个函数中,在以下情况下,该函数是authorize()
。另外,我正在使用一个全局驱动器实例,因为下面的方法是众多方法之一
public static String insertFile(String folderId, java.io.File file) throws IOException, GeneralSecurityException {
System.out.println("Uploading file " + file.getName() + " to Google Drive");
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// authorization
Credential credential = authorize(); // Get authorization
// set up the global Drive instance
drive = new Drive.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName("YourAppName")
.build();
File fileMetadata = new File();
fileMetadata.setName(file.getName());
FileContent mediaContent = new FileContent("put/mime/type/here", file);
File uploadedFile;
if (folderId != null && !folderId.isEmpty()) {
fileMetadata.setParents(Collections.singletonList(folderId));
uploadedFile = drive.files()
.create(fileMetadata, mediaContent)
.setFields("id, parents")
.execute();
} else {
uploadedFile = drive.files()
.create(fileMetadata, mediaContent)
.setFields("id")
.execute();
}
return uploadedFile == null ? null : uploadedFile.getId();
}
上述方法将folderId
和要上载的文件作为参数,如果上载成功,则返回fileId
如果将null
或空字符串传递到folderId
,则文件将上载到根目录中。谢谢您的帮助..但我怀疑parentId是否表示文件夹名称或id。如果id表示如何从驱动器文件夹名称获取id?父id是否表示文件夹名称,这应该会让您了解如何从驱动器获取信息。好的,我有文件夹名称如何获取此名称的id。可能使用q命令搜索标题,但我们问题的答案仍然是。将父id添加到主体中。