【问题标题】:Google Drive Api Java Quickstart compile errorGoogle Drive Api Java Quickstart 编译错误
【发布时间】:2017-01-09 16:31:19
【问题描述】:

我正在尝试使用 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());
            }
        }
    }

}

【问题讨论】:

  • 提示:粘贴编译器错误是解决此类问题的一个重要部分。另一部分(缺失)......给出问题的源代码!如果您希望我们帮助您,请添加缺少的部分...
  • 其他重要的东西:提出问题后要在身边。
  • 感谢您的回复 GhostCat。请为无法使用而道歉。
  • 格式化问题,并删除不必要的文本

标签: java api google-drive-api


【解决方案1】:

我不能告诉你到底出了什么问题;解释一下发生了什么。

示例 - 编译器抱怨:

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

对应的代码是:

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

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

list() 应该返回一个List 类的对象。

现在您转到该类的 javadoc ... 并惊讶:有 no 方法称为 setPageSize()

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

  1. 就像我做的那样:深入那个电话的深处
  2. 查看该类的文档

为了弄清楚发生了什么。

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

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

  1. 确保示例代码匹配您正在使用的 API 版本
  2. 相信编译器消息。

说真的:如果您需要其他人向您解释这些基本的东西,那么您应该首先花几个小时来了解它们;例如,通过学习一些基本的 Java 教程。当您需要其他人了解正在发生的事情时,尝试使用高级 Google API 毫无意义。

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

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

【讨论】:

  • 感谢您的信息。不是 Java 开发人员,但有足够的知识提出问题,希望有人能提供帮助。
  • 不客气;如果您觉得我的回答足够,请考虑在某个时候接受。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2018-06-09
    • 2013-07-23
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多