【问题标题】:Google Drive API Upload returns File ID/Title but document does not exist in DriveGoogle Drive API Upload 返回文件 ID/标题,但文件在 Drive 中不存在
【发布时间】:2012-11-08 16:50:15
【问题描述】:

我已按照 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 类型:文本/纯文本,但驱动器中不存在该文档,并且没有返回错误。

我已经为此工作了 2 天,但没有成功,非常感谢任何帮助。我在网上查了一下,发现的例子和下面类似。我尝试了多个 Google 帐户(一个是公司 Google Apps,另一个是具有相同结果的普通 gmail 帐户)。

代码(更改了帐户信息):

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); 
} 

}
}

谢谢,

乔·史密斯

【问题讨论】:

    标签: google-drive-api


    【解决方案1】:

    使用服务帐户时,插入的文件将被添加到没有云端硬盘用户界面的应用程序的云端硬盘帐户中。这些文件只能通过 API 获得。

    【讨论】:

    • 嗨克劳迪奥,谢谢。我的用例是我运行自动化性能测试,并希望使用 API 将测试结果上传到 Google Drive 并将它们发布给利益相关者。我认为 API 服务帐户将是最好的框架。从 API 文档 developers.google.com/drive/…> 中,它提到“执行“管理驱动器文件”中描述的任何其他操作。我认为这意味着一旦上传了文档,我就可以与其他人分享它。你有什么方法可以推荐这个?
    • 如果您与其他用户共享该文档,它将显示在他们的云端硬盘 UI 中,不是这样吗?
    • 您好克劳迪奥,再次感谢您。我在网上做了一些研究,并按照权限指南:插入 developers.google.com/drive/v2/reference/permissions/insert>,现在共享按预期工作。我的问题现在已经解决了。 :)
    • 我认为每当您创建服务帐户时,您就是在模拟域中的用户——那么文件肯定应该显示在该用户的云端硬盘 UI 中吗?
    • 实际上花了一整天的时间才想弄清楚这一点。感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 2021-07-23
    • 2014-11-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多