谷歌驱动器api服务帐户使用java查看上传的文件到谷歌驱动器

时间:2014-08-14 07:54:00

标签: java oauth-2.0 google-drive-api

我创建了使用java中的服务帐户将文件插入谷歌驱动器的代码。文件上传到谷歌驱动器但我无法使用网络浏览器看到它。当我使用代码检查文件时,我可以检索上传的文件.my question我如何将文件上传到谷歌驱动器,可以使用谷歌中的谷歌服务帐户通过网络浏览器查看

请在下面找到示例代码。

      public class PlusServiceAccountSample {


          private static final String APPLICATION_NAME = "";

          /** E-mail address of the service account. */
          private static final String SERVICE_ACCOUNT_EMAIL = "XXXXXXXX@developer.gserviceaccount.com" ;

          /** Global instance of the HTTP transport. */
          private static HttpTransport httpTransport;

          /** Global instance of the JSON factory. */
          private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();


          public static void main(String[] args) {
            try {
              try {
                httpTransport = GoogleNetHttpTransport.newTrustedTransport();
                // check for valid setup
                if (SERVICE_ACCOUNT_EMAIL.startsWith("Enter ")) {
                  System.err.println(SERVICE_ACCOUNT_EMAIL);
                  System.exit(1);
                }
                String p12Content = Files.readFirstLine(new File("XXXXX-privatekey.p12"), Charset.defaultCharset());
                if (p12Content.startsWith("Please")) {
                  System.err.println(p12Content);
                  System.exit(1);
                }
                // service account credential (uncomment setServiceAccountUser for domain-wide delegation)
                GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
                    .setJsonFactory(JSON_FACTORY)
                    .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                    .setServiceAccountScopes(Collections.singleton(DriveScopes.DRIVE))
                    .setServiceAccountPrivateKeyFromP12File(new File("XXXXXXXX-privatekey.p12"))

                    .build();


                Drive service = new Drive.Builder(httpTransport, JSON_FACTORY,credential).build();

                com.google.api.services.drive.model.File  file = new com.google.api.services.drive.model.File();
                file.setTitle("testdd");
                file.setMimeType("application/vnd.google-apps.spreadsheet");
                Drive.Files.Insert insert = null;
                try {
                    insert = service.files().insert(file);
                    file = insert.execute();


                } catch (Exception e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

                com.google.api.services.drive.model.File uploadedDoc = service.files().get("1ZJnkgFXAiUNSBeq3DmhHGqXn7-v37vLg1UsRekRQNjU").execute();

                System.out.println("Title: " + uploadedDoc.getTitle());




                return;
              } catch (IOException e) {
                System.err.println(e.getMessage());
              }
            } catch (Throwable t) {
              t.printStackTrace();
            }
            System.exit(1);
          }

2 个答案:

答案 0 :(得分:2)

如果您为该文件设置了权限,则可以在常规Gmail帐户中查看该文件("与我共享"部分):

Permission newPermission = new Permission();
newPermission.setValue("...your regular username...@gmail.com");
newPermission.setType("user");
newPermission.setRole("reader");
service.permissions().insert(file.getId(), newPermission).execute();

您还可以使用唯一网址转到该文件:

System.out.println("Title: " + uploadedDoc.getTitle()+ " "+ uploadedDoc.getAlternateLink());

“类型”和“角色”的可能值位于Google云端硬盘SDK文档中: https://developers.google.com/drive/v2/reference/permissions/insert

答案 1 :(得分:2)

  1. 转到Google Google云端硬盘。创建一些文件夹,然后打开“共享” 设置“为它。
  2. 将您的服务电子邮件添加到允许的用户列表中并允许其发送 编辑该文件夹的内容。
  3. 在某处记住文件夹ID,并在上传时使用此文件夹ID 文件。
  4. 它将上传到您的个人google云端硬盘帐户。