Google drive api Google驱动器API上载返回文件ID/标题,但驱动器中不存在文档
我遵循Google Drive SDK站点上给出的示例,通过服务帐户进行授权(https://developers.google.com/drive/service-accounts)并插入一个文件(https://developers.google.com/drive/v2/reference/files/insert). 我已经通过oauth2使用客户机ID/客户机机密成功地使其工作,但是需要自动化,所以我想使用私钥 我的问题是,我得到了一个文件id、标题、说明和MIME类型作为回报,例如文件id:%s0B6ysbMIcH3AGWHJPRmZUTVZZMnM,标题:我的文档,说明:测试文档,MIME类型:text/plain,但该文档在驱动器中不存在,并且不会返回任何错误 我已经在这方面工作了2天,但没有成功,我真的非常感谢任何帮助。我在网上看过,我发现的例子与下面的相似。我尝试了多个谷歌账户(一个是公司谷歌应用,另一个是普通的gmail账户,结果相同) 代码(帐户信息已更改): 谢谢Google drive api Google驱动器API上载返回文件ID/标题,但驱动器中不存在文档,google-drive-api,Google Drive Api,我遵循Google Drive SDK站点上给出的示例,通过服务帐户进行授权(https://developers.google.com/drive/service-accounts)并插入一个文件(https://developers.google.com/drive/v2/reference/files/insert). 我已经通过oauth2使用客户机ID/客户机机密成功地使其工作,但是需要自动化,所以我想使用私钥 我的问题是,我得到了一个文件id、标题、说明和MIME类型作为回报,例如文
Joe Smith使用服务帐户时,插入的文件将添加到没有驱动器UI的应用程序驱动器帐户中。这些文件只能通过API获得。你好,克劳迪奥,谢谢。我的用例是运行自动性能测试,并希望使用API将测试结果上传到Google Drive中,并将其发布给利益相关者。我认为API服务帐户将是最好的框架。API文档中提到“执行管理驱动器文件”中描述的任何其他操作。“我想这意味着一旦上传了文档,我就可以与其他人共享它。您有什么建议吗?如果您与其他用户共享文档,文档将显示在他们的驱动器UI中,不是吗?嗨,Claudio,再次感谢您。我在网上做了一些研究,并遵循权限指南:insert,现在让共享按预期工作。我的问题现在已经解决了。:)我认为,每当你创建一个服务帐户时,你都会在域中模拟一个用户——那么文件肯定会显示在该用户的驱动器UI中吗?实际上,你花了一整天的时间才试图弄明白这一点。谢谢你的回答。
public class AutoGoogleDrive {
private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "/home/jsmith/Java/11111111111111111111111111-privatekey.p12";
private static final String SERVICE_ACCOUNT_EMAIL = "1111111111111@developer.gserviceaccount.com";
public static Drive getDriveService() throws GeneralSecurityException,
IOException, URISyntaxException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(DriveScopes.DRIVE_FILE)
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.build();
Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
.setHttpRequestInitializer(credential).build();
return service;
}
public static void main(String[] args) throws IOException {
Drive service = null;
try {
service = getDriveService();
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Insert a text file
File body = new File();
body.setTitle("My document");
body.setDescription("A test document");
body.setMimeType("text/plain");
// File's content.
java.io.File fileContent = new java.io.File("/home/jsmith/document.txt");
FileContent mediaContent = new FileContent("text/plain", fileContent);
try {
File file = service.files().insert(body, mediaContent).execute();
// Uncomment the following line to print the File ID.
System.out.println("File ID: %s" + file.getId());
System.out.println("Title: " + file.getTitle());
System.out.println("Description: " + file.getDescription());
System.out.println("MIME type: " + file.getMimeType());
} catch (IOException e) {
System.out.println("An error occured: " + e);
}
}
}