【问题标题】:Upload file from google app engine to google drive using java使用java将文件从谷歌应用引擎上传到谷歌驱动器
【发布时间】:2015-09-07 13:50:18
【问题描述】:

我已成功收到来自电子邮件的 pdf 文件,但我必须将该 Pdf 文件存储到谷歌驱动器中。这是我的代码

private static void uploadFile(InputStream is) {

             com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
             fileMetadata.setTitle(fileName);
             fileMetadata.setMimeType("application/pdf");

             HttpTransport httpTransport = new NetHttpTransport();
             JsonFactory jsonFactory = new JacksonFactory();
             AppIdentityCredential credential = new AppIdentityCredential.Builder(Arrays.asList(DriveScopes.DRIVE)).build();
             // API_KEY is from the Google Console as a server API key
             GoogleClientRequestInitializer keyInitializer = new CommonGoogleClientRequestInitializer("My API_key here");
             Drive service = new Drive.Builder(httpTransport, jsonFactory, null)

                 .setApplicationName("My app name here")
                 .setHttpRequestInitializer(credential)
                 .setGoogleClientRequestInitializer(keyInitializer)
                 .build();

             try {
                Drive.Files.Insert in= service.files().insert(fileMetadata, new InputStreamContent("application/pdf",is));
                com.google.api.services.drive.model.File retFile =in.execute();
                if(retFile!=null){
                    System.out.println("Drive retFile:"+retFile.getTitle());    
                }
            } catch (IOException e) {
                System.out.println("Drive Error:"+e.toString());
            }
          }

我已检查关联帐户 Google Drive 没有上传任何内容。

过去 1 周我在 Google 上搜索过,但没有找到将文件从 GAE 上传到 Google Dive 的任何明确文档。

提前谢谢...

【问题讨论】:

  • 很高兴您发布了您的解决方案。这对未来的用户非常有帮助。您认为您可以将您的解决方案作为您自己问题的“答案”发布吗?这通常是在stackoverflow上完成的。祝你有美好的一天!

标签: java google-app-engine oauth google-drive-api


【解决方案1】:

我终于找到了解决方案,首先我们需要了解 GAE 服务帐户和关联的 Google 帐户有单独的驱动器存储。

我上面的代码将文件存储到 GAE 服务帐户驱动器(没有 UI 的服务帐户),因此我们关联的 Google 帐户看不到它。

所以我们需要将服务帐户文件共享给关联的 Google 帐户。这是我修改后的代码

private static void uploadFile(InputStream is) {

            com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
            fileMetadata.setTitle(curDateWithFormat()+"-"+fileName);
            fileMetadata.setMimeType("application/pdf");


             HttpTransport httpTransport = new NetHttpTransport();
             JsonFactory jsonFactory = new JacksonFactory();
             AppIdentityCredential credential = new AppIdentityCredential.Builder(Arrays.asList(DriveScopes.DRIVE)).build();
             // API_KEY is from the Google Console as a server API key
             GoogleClientRequestInitializer keyInitializer = new CommonGoogleClientRequestInitializer("AIzaSyCKLTs_D2zYJepjno8OSVy-2CItYayvu0M");
             Drive service = new Drive.Builder(httpTransport, jsonFactory, null)

                 .setApplicationName("TimesheetPDFParser")
                 .setHttpRequestInitializer(credential)
                 .setGoogleClientRequestInitializer(keyInitializer)
                 .build();

             try {
                Drive.Files.Insert in= service.files().insert(fileMetadata, new InputStreamContent("application/pdf",is));
                com.google.api.services.drive.model.File retFile =in.execute();

                if(retFile!=null){
                    Permission newPermission = new Permission();
                    newPermission.setValue("here your email id @gmail.com");
                    newPermission.setType("user");
                    newPermission.setRole("reader");//owner and writer role throws 400 BAD_REQUEST exception    
                    service.permissions().insert(retFile.getId(), newPermission).execute();

                    System.out.println("Drive retFile:"+retFile.getId());   
                }
            } catch (IOException e) {
                System.out.println("Drive Error:"+e.toString());
            }
          }    

现在您可以在与我共享选项中的共享帐户驱动器中查看您的文件。

引用自:

https://developers.google.com/drive/v2/reference/permissions/insert

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 2020-03-13
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多