Google Drive Api Java快速入门编译错误

时间:2017-01-09 16:31:19

标签: java api google-drive-api

我正在尝试使用Google Drive Service Java API上传文件。我使用Quickstart.java示例来确定我需要编码的内容。我收到编译器错误。请协助解决我的编译器错误。

Quickstart.java:107: error: cannot find symbol
             .setPageSize(10)
             ^
  symbol:   method setPageSize(int)
  location: class Drive.Files.List
Quickstart.java:110: error: cannot find symbol
        List<File> files = result.getFiles();
                                 ^
  symbol:   method getFiles()
  location: variable result of type FileList
Quickstart.java:116: error: cannot find symbol
                System.out.printf("%s (%s)\n", file.getName(), file.getId());
                                                                   ^
  symbol:   method getId()
  location: variable file of type File
3 errors

我可能正在使用过时的库,但我去了Google的REST网站 并下载他们拥有的最新产品。

以下是代码:

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.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;

import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.*;
import com.google.api.services.drive.Drive;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class Quickstart {
    /** Application name. */
    private static final String APPLICATION_NAME =
        "Drive API Java Quickstart";

    /** Directory to store user credentials for this application. */
    private static final java.io.File DATA_STORE_DIR = new java.io.File(
        System.getProperty("user.home"), ".credentials/drive-java-quickstart");

    /** Global instance of the {@link FileDataStoreFactory}. */
    private static FileDataStoreFactory DATA_STORE_FACTORY;

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

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

    /** Global instance of the scopes required by this quickstart.
     *
     * If modifying these scopes, delete your previously saved credentials
     * at ~/.credentials/drive-java-quickstart
     */
    private static final List<String> SCOPES =
        Arrays.asList(DriveScopes.DRIVE_METADATA_READONLY);

    static {
        try {
            HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
        } catch (Throwable t) {
            t.printStackTrace();
            System.exit(1);
        }
    }

    /**
     * Creates an authorized Credential object.
     * @return an authorized Credential object.
     * @throws IOException
     */
    public static Credential authorize() throws IOException {
        // Load client secrets.
        InputStream in =
            Quickstart.class.getResourceAsStream("/client_secret.json");
        GoogleClientSecrets clientSecrets =
            GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow =
                new GoogleAuthorizationCodeFlow.Builder(
                        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(DATA_STORE_FACTORY)
                .setAccessType("offline")
                .build();
        Credential credential = new AuthorizationCodeInstalledApp(
            flow, new LocalServerReceiver()).authorize("user");
        System.out.println(
                "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
        return credential;
    }

    /**
     * Build and return an authorized Drive client service.
     * @return an authorized Drive client service
     * @throws IOException
     */
    public static Drive getDriveService() throws IOException {
        Credential credential = authorize();
        return new Drive.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME)
                .build();
    }

    public static void main(String[] args) throws IOException {
        // Build a new authorized API client service.
        Drive service = getDriveService();

        // Print the names and IDs for up to 10 files.
        FileList result = service.files().list()
             .setPageSize(10)
             .setFields("nextPageToken, files(id, name)")
             .execute();
        List<File> files = result.getFiles();
        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.getName(), file.getId());
            }
        }
    }

}

2 个答案:

答案 0 :(得分:0)

我无法告诉你到底出了什么问题;只是解释发生了什么。

示例 - 编译器抱怨:

Quickstart.java:107: error: cannot find symbol
         .setPageSize(10)

相应的代码是:

FileList result = service.files().list()
         .setPageSize(10)

让我们看看你得到了什么。 service是Google API的云端硬盘类的一个实例。当你开始研究javadoc时,那个类提供了一个方法files();以及该调用的结果......允许调用list()。

并且list()应该返回类List的对象。

现在你转向该类的javadoc ......并且惊讶:没有方法名为 setPageSize()

所以,长话短说:编译器告诉你,你试图调用某个对象的方法并不存在!你的反应是:

  1. 就像我做的那样:潜入那个电话的深处
  2. 查看该课程的文档
  3. 为了弄清楚发生了什么。

    我不是Google API的专家,但我的猜测是:这是某种版本的不兼容性。可能是您从某个地方复制了Quickstart源代码...但源代码与您的编译器在项目设置中看到的Google API版本完全不兼容。

    所以,这里真正的答案是:

    1. 确保示例代码匹配您正在使用的API版本
    2. 信任编译器消息。
    3. 严肃地说:如果你需要其他人向你解释这些基本的东西,那么你应该首先花一些时间来了解它们;例如,通过一些基本的Java教程。当您需要其他人了解正在发生的事情时,尝试使用高级Google API是毫无意义的。

      除此之外:你从不只是从某个地方放下一些代码。在尝试运行之前你做的最小的事情:完全阅读;并了解那里的每一个电话。

      我希望这能为您提供足够的想法来解决您的实际问题。

答案 1 :(得分:0)

我遇到了同样的问题然后意识到我不小心下载了V2库而不是V3库。

确保您已下载最新的库(截至此帖可在Google Drive Platform: Downloads中找到。)