Google云端硬盘API上传会返回文件ID /标题,但云端硬盘中不存在文档 [英] Google Drive API Upload returns File ID/Title but document does not exist in Drive

查看:215
本文介绍了Google云端硬盘API上传会返回文件ID /标题,但云端硬盘中不存在文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Google Drive SDK网站上的示例,通过服务帐户(https://developers.google.com/drive/service-accounts)查看授权并插入文件(https://developers.google .COM /驱动器/ V2 /参考/文件/插入)。我已经设法使用oauth2的客户端ID /客户机秘密工作,但需要自动化,因此需要使用私钥。

I have followed the examples given on the Google Drive SDK site for Authorization via Service Accounts (https://developers.google.com/drive/service-accounts) and to insert a file (https://developers.google.com/drive/v2/reference/files/insert). I have managed to get it working using the Client ID/Client secret with oauth2 but need automation so want to use the private key.

我的问题是我给了一个文件ID,标题,描述& MIME类型,例如返回文件ID:%s0B6ysbMIcH3AGWHJPRmZUTVZZMnM,标题:我的文档,说明:测试文档,MIME类型:text / plain,但文档不存在于驱动器中,并且不返回错误。

My issue is I am given a file id, Title, Description & MIME type in return e.g. File ID: %s0B6ysbMIcH3AGWHJPRmZUTVZZMnM, Title: My document, Description: A test document, MIME type: text/plain but the document does -not- exist in Drive and no errors are returned.

我一直在为此工作2天而没有成功,并且非常感谢任何帮助。我已经看了在线,我发现的例子与下面类似。我尝试了多个Google帐户(其中一个是Google Apps公司,另一个是正常的Gmail帐户,结果相同)。

I have been work on this for 2 days without success and would really appreciate any assistance. I have looked on-line and the examples I have found are similar to the below. I have tried multiple Google accounts (one a company Google Apps & another a normal gmail account with the same result).

代码(包含帐户信息已更改):

The code (with the account info changed) :

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

}
}

Joe Smith

Joe Smith

推荐答案

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

When using service accounts, the inserted file will be added to the application's Drive account for which there's no Drive UI. Those files are only available through the API.

这篇关于Google云端硬盘API上传会返回文件ID /标题,但云端硬盘中不存在文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆