Google云端硬盘API未列出我的所有文件

时间:2016-06-02 09:08:03

标签: java google-drive-api

我使用非常简单的示例来检索谷歌硬盘上的文件。有两件奇怪的事情

  1. 第一次运行示例时,google说"应用程序希望访问您的文件blahblah"。好吧,我确认了。但是,第二次,它说"应用程序想要离线访问" ......啊......为什么?

  2. 可能(但我不确定),在我第二次确认之后,我只收到我通过此应用程序上传的文件

  3. 代码是

    package com.google.api.services.samples.drive.cmdline;
    
    import com.google.api.client.auth.oauth2.Credential;
    import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
    import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
    import com.google.api.client.googleapis.GoogleUtils;
    import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
    import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
    import com.google.api.client.googleapis.media.MediaHttpDownloader;
    import com.google.api.client.googleapis.media.MediaHttpUploader;
    import com.google.api.client.http.FileContent;
    import com.google.api.client.http.GenericUrl;
    import com.google.api.client.http.HttpTransport;
    import com.google.api.client.http.javanet.NetHttpTransport;
    import com.google.api.client.json.JsonFactory;
    import com.google.api.client.json.jackson2.JacksonFactory;
    import com.google.api.client.util.Preconditions;
    import com.google.api.client.util.store.DataStoreFactory;
    import com.google.api.client.util.store.FileDataStoreFactory;
    import com.google.api.services.drive.Drive;
    import com.google.api.services.drive.DriveScopes;
    import com.google.api.services.drive.model.File;
    import com.google.api.services.drive.model.FileList;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.util.Collections;
    import java.util.List;
    
    /**
     * A sample application that runs multiple requests against the Drive API. The requests this sample
     * makes are:
     * <ul>
     * <li>Does a resumable media upload</li>
     * <li>Updates the uploaded file by renaming it</li>
     * <li>Does a resumable media download</li>
     * <li>Does a direct media upload</li>
     * <li>Does a direct media download</li>
     * </ul>
     *
     * @author rmistry@google.com (Ravi Mistry)
     */
    public class DriveSample {
    
      /**
       * Be sure to specify the name of your application. If the application name is {@code null} or
       * blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0".
       */
      private static final String APPLICATION_NAME = "testing";
    
      private static final String UPLOAD_FILE_PATH = "c:\\Users\\bnie\\workspace\\Servers\\.project";
      private static final String DIR_FOR_DOWNLOADS = "c:\\temp";
      private static final java.io.File UPLOAD_FILE = new java.io.File(UPLOAD_FILE_PATH);
    
      /** Directory to store user credentials. */
      private static final java.io.File DATA_STORE_DIR =
          new java.io.File(System.getProperty("user.home"), ".store/drive_sample");
    
      /**
       * Global instance of the {@link DataStoreFactory}. The best practice is to make it a single
       * globally shared instance across your application.
       */
      private static FileDataStoreFactory dataStoreFactory;
    
      /** Global instance of the HTTP transport. */
      private static HttpTransport httpTransport;
    
      /** Global instance of the JSON factory. */
      private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    
      /** Global Drive API client. */
      private static Drive drive;
    
      /** Authorizes the installed application to access user's protected data. */
      private static Credential authorize() throws Exception {
        // load client secrets
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(DriveSample.class.getResourceAsStream("/client_secrets.json")));
        if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
          System.out.println(
              "Enter Client ID and Secret from https://code.google.com/apis/console/?api=drive "
                  + "into drive-cmdline-sample/src/main/resources/client_secrets.json");
          System.exit(1);
        }
        // set up authorization code flow
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport,
            JSON_FACTORY, clientSecrets, Collections.singleton(DriveScopes.DRIVE_FILE))
                .setDataStoreFactory(dataStoreFactory).build();
        // authorize
        return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
      }
    
      public static void main(String[] args) {
        Preconditions.checkArgument(
            !UPLOAD_FILE_PATH.startsWith("Enter ") && !DIR_FOR_DOWNLOADS.startsWith("Enter "),
            "Please enter the upload file path and download directory in %s", DriveSample.class);
    
        try {
          // httpTransport = GoogleNetHttpTransport.newTrustedTransport();
          NetHttpTransport.Builder builder = new NetHttpTransport.Builder();
          builder.trustCertificates(GoogleUtils.getCertificateTrustStore());
          httpTransport = builder.build();
          dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
          // authorization
          Credential credential = authorize();
          // set up the global Drive instance
          drive = new Drive.Builder(httpTransport, JSON_FACTORY, credential)
              .setApplicationName(APPLICATION_NAME).build();
          FileList l = drive.files().list().setMaxResults(10).execute();
          List<File> files = l.getItems();
    
          if (files == null || files.size() == 0) {
            System.out.println("No files found.");
          } else {
            System.out.println("Files:");
            for (File file : files) {
              System.out.printf("%s (%s)\n", file.getTitle(), file.getId());
            }
          }
          // run commands
    
          // View.header1("Starting Resumable Media Upload");
          // File uploadedFile = uploadFile(false);
          //
          // View.header1("Updating Uploaded File Name");
          // File updatedFile = updateFileWithTestSuffix(uploadedFile.getId());
          //
          // View.header1("Starting Resumable Media Download");
          // downloadFile(false, updatedFile);
          //
          // View.header1("Starting Simple Media Upload");
          // uploadedFile = uploadFile(true);
          //
          // View.header1("Starting Simple Media Download");
          // downloadFile(true, uploadedFile);
          //
          // View.header1("Success!");
          return;
        } catch (IOException e) {
          System.err.println(e.getMessage());
        } catch (Throwable t) {
          t.printStackTrace();
        }
        System.exit(1);
      }
    
      /** Uploads a file using either resumable or direct media upload. */
      private static File uploadFile(boolean useDirectUpload) throws IOException {
        File fileMetadata = new File();
        fileMetadata.setTitle(UPLOAD_FILE.getName());
    
        FileContent mediaContent = new FileContent("image/jpeg", UPLOAD_FILE);
    
        Drive.Files.Insert insert = drive.files().insert(fileMetadata, mediaContent);
        MediaHttpUploader uploader = insert.getMediaHttpUploader();
        uploader.setDirectUploadEnabled(useDirectUpload);
        uploader.setProgressListener(new FileUploadProgressListener());
        return insert.execute();
      }
    
      /** Updates the name of the uploaded file to have a "drivetest-" prefix. */
      private static File updateFileWithTestSuffix(String id) throws IOException {
        File fileMetadata = new File();
        fileMetadata.setTitle("drivetest-" + UPLOAD_FILE.getName());
    
        Drive.Files.Update update = drive.files().update(id, fileMetadata);
        return update.execute();
      }
    
      /** Downloads a file using either resumable or direct media download. */
      private static void downloadFile(boolean useDirectDownload, File uploadedFile)
          throws IOException {
        // create parent directory (if necessary)
        java.io.File parentDir = new java.io.File(DIR_FOR_DOWNLOADS);
        if (!parentDir.exists() && !parentDir.mkdirs()) {
          throw new IOException("Unable to create parent directory");
        }
        OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getTitle()));
    
        MediaHttpDownloader downloader =
            new MediaHttpDownloader(httpTransport, drive.getRequestFactory().getInitializer());
        downloader.setDirectDownloadEnabled(useDirectDownload);
        downloader.setProgressListener(new FileDownloadProgressListener());
        downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);
      }
    }
    

1 个答案:

答案 0 :(得分:1)

尝试更改您使用的范围。使用范围DriveScopes.DRIVE代替DriveScopes.DRIVE_FILE。有关范围的更多信息,请查看此documentation

同时检查此相关SO question