2017-01-09 85 views
1

我试图使用Google Drive Service Java API上传文件。我正在使用一个Quickstart.java示例来确定我需要编码的内容。我收到一个编译器错误。请协助解决我的编译器错误。Google Drive Api 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()); 
      } 
     } 
    } 

} 
+1

提示(因为这个职位可以在Google Drive Platform: Downloads被发现。):粘贴的编译器错误是解决这些问题的一个基本组成部分。另一部分(缺少)......给出问题的源代码!如果您希望我们帮助您,请添加缺少的部分... – GhostCat

+0

**其他**重要的必备事项:在提出问题之后解决。 – GhostCat

+0

感谢您的回应GhostCat。为无法使用而道歉。 –

回答

0

我不能告诉你究竟是错误的;只是解释发生了什么。

例子 - 编译器抱怨:

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

相应的代码是:

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

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

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

现在你转向的Javadoc该类......和惊喜:有没有方法称为setPageSize()

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

  1. 像我一样:潜入该呼叫
  2. 看深处到文档该类

为了弄清楚到底是怎么回事。

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

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

  1. 确保示例代码比赛您使用的是
  2. 信托编译器的消息API版本。

并认真:如果您需要其他人来解释这些基本的东西给你,那么你应该先了解他们一些时间;例如通过编写一些基本的Java教程。尝试使用高级Google API时没有意义,因为当您需要其他人了解时发生了什么。

此外:你从来没有只是从某处放下一些代码。你甚至在尝试运行之前所做的最低限度的事情:完全阅读;并了解那里的每一个电话。

我希望这给你足够的想法来解决你的实际问题。

+0

感谢您的信息。不是一个Java开发人员,但足够知道可以提出一个问题,希望有人能够提供帮助。 –

+0

不客气,如果您发现我的答案充足,请考虑在某个时候接受。 – GhostCat

0

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

确保你已经下载了最新的库