繁体   English   中英

使用Java中的Google Drive SDK API显示文件列表

[英]Display the list of files using Google Drive SDK API in Java

我无法修复...下面您可以找到代码:

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
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.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class getDriveService {


    /** Email of the Service Account */
    private static final String SERVICE_ACCOUNT_EMAIL = "<email_address_service>@developer.gserviceaccount.com";

    /** Path to the Service Account's Private Key file */
    private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "src/<file_key>-privatekey.p12";

    public static void main(String[] args) throws IOException, GeneralSecurityException, URISyntaxException {

        List<File> lista = null;

        lista = retrieveAllFiles(getDriveService());

        System.out.println(""+lista.size());
        for (File file : lista) {
            System.out.println(""+file.getId());
        }
    }

    /**
     * Build and returns a Drive service object authorized with the service accounts
     * that act on behalf of the given user.
     *
     * @param userEmail The email of the user.
     * @return Drive service object that is ready to make requests.
     */
    public static Drive getDriveService() throws GeneralSecurityException,
        IOException, URISyntaxException {

        Collection<String> elenco = new ArrayList<String>();
        elenco.add("https://www.googleapis.com/auth/drive");

        HttpTransport httpTransport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();
        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                .setServiceAccountScopes(elenco)
                .setServiceAccountPrivateKeyFromP12File(
                        new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
                .build();
        Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
                .setApplicationName("FileListAccessProject")
                .setHttpRequestInitializer(credential).build();

        return service;
}

    /**
       * Retrieve a list of File resources.
       *
       * @param service Drive API service instance.
       * @return List of File resources.
       */
      private static List<File> retrieveAllFiles(Drive service) throws IOException {
        List<File> result = new ArrayList<File>();
        Files.List request = service.files().list();

        do {
          try {
            FileList files = request.execute();

            result.addAll(files.getItems());
            request.setPageToken(files.getNextPageToken());
          } catch (IOException e) {
            System.out.println("An error occurred: " + e);
            request.setPageToken(null);
          }
        } while (request.getPageToken() != null &&
                 request.getPageToken().length() > 0);

        return result;
      }


}

如果运行main方法,它将显示“ 0”。 文件列表为空。 为什么?

该代码似乎正确,我还从Google API控制台面板启用了服务“ Drive API” ...,并且我禁用了“ Drive SDK”(我不明白为什么,但是在这里https://developers.google.com / drive /代表这样说)

有什么事吗 我忘记了什么吗?

预先感谢您的任何帮助

弗朗切斯科

您必须将ServiceAccountUser设置为您要访问的用户驱动器,并将ServiceAccountId设置为Service Account用户。

像这样的东西:

GoogleCredential credentialUser = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountUser("user@gmail.com")
.setServiceAccountPrivateKeyFromP12File(new File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.setServiceAccountScopes(serviceAccountScopes).build();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM