簡體   English   中英

Google Drive Api Java Quickstart編譯錯誤

[英]Google Drive Api Java Quickstart compile error

我正在嘗試使用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

我可能正在使用一個過時的庫,但我去了谷歌的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());
            }
        }
    }

}

我不能告訴你到底出了什么問題; 只是解釋發生了什么。

示例 - 編譯器抱怨:

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 ......並且驚訝: 沒有名為setPageSize()的方法。

所以,長話短說:編譯器告訴你,你試圖調用某個對象的方法不存在! 你的反應是:

  1. 就像我做的那樣:深入了解那個電話的深度
  2. 查看該課程的文檔

為了弄清楚發生了什么。

我不是Google API的專家,但我的猜測是:這是某種版本的不兼容性。 可能是您從某個地方復制了Quickstart源代碼...但是源代碼與您的編譯器在項目設置中看到的Google API版本完全不兼容。

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

  1. 確保示例代碼您使用的API版本匹配
  2. 信任編譯器消息。

並且認真地說:如果你需要其他人向你解釋這些基本的東西,那么你應該花一些時間來了解它們; 例如,通過一些基本的Java教程。 當您需要其他人了解正在發生的事情時,嘗試使用高級Google API是沒有意義的。

除此之外:你永遠不會從某個地方放下一些代碼。 在嘗試運行之前你做的最小的事情:完全閱讀; 並了解那里的每一個電話。

我希望這能給你足夠的想法來解決你的實際問題。

我遇到了同樣的問題然后意識到我不小心下載了V2庫而不是V3庫。

確保您已下載最新的庫(截至此帖子可在Google雲端硬盤平台:下載中找到 。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM