2014-10-06 48 views
-2

这是我的代码。线程“main”中的异常java.lang.Error:未解决的编译问题gdrive

我的程序将文件上传到的GDrive:

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; 
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; 
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse; 
import com.google.api.client.http.FileContent; 
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.jackson2.JacksonFactory; 
import com.google.api.services.drive.Drive; 
import com.google.api.services.drive.DriveScopes; 
import com.google.api.services.drive.model.File; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.Arrays; 

// main function 
public class DriveCommandLine 
{ 
// providing id and user 
    private static String CLIENT_ID = "686430-a05tdl7fbkrkuf3cd1029ivhst8rocuo.apps.googleusercontent.com"; 
    private static String CLIENT_SECRET = "nI-SthRC4UU0P__ow"; 

    private static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob"; 

    public static void main(String[] args) throws IOException 
    { 
    HttpTransport httpTransport = new NetHttpTransport(); 
    JsonFactory jsonFactory = new JacksonFactory(); 

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE)).setAccessType("online").setApprovalPrompt("auto").build(); 

    String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build(); 
    System.out.println("Please open the following URL in your browser then type the authorization code:"); 
    System.out.println(" " + url); 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    String code = br.readLine(); 

    GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute(); 
    GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response); 

    //Create a new authorized API client 
    Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build(); 

    //Insert a file 
    File body = new File(); 
    body.setTitle("My document"); 
    body.setDescription("A test document"); 
    body.setMimeType("text/plain"); 

    java.io.File fileContent = new java.io.File("document.txt"); 
    FileContent mediaContent = new FileContent("text/plain", fileContent); 

    File file = service.files().insert(body, mediaContent).execute(); 
    System.out.println("File ID: " + file.getId()); 
    } 
} 

收到错误为:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: DriveScopes cannot be resolved to a variable Drive cannot be resolved to a type Drive cannot be resolved to a type File cannot be resolved to a type File cannot be resolved to a type File cannot be resolved to a type

at gdrive.DriveCommandLine.main(DriveCommandLine.java:33)

任何一个能帮助我吗?

+0

正确地包含jar文件以供导入和编译。 – Karthik 2014-10-06 10:37:18

+0

你已经包括“谷歌的API - 服务驱动器V2的[版本] .jar”,,,然后得到错误..! – jai 2014-10-06 10:40:04

+0

我提供的想法对你有帮助吗? – 2014-10-07 07:39:58

回答

1

假设在执行你的应用程序你的错误你使用Eclipse的工作,这应该解决您的问题:

  1. 从构建路径
  2. 删除您的第三方库,创建一个文件夹你的Android项目称为libs
  3. 复制您的第三方库到新创建的文件夹
  4. 复制库添加到构建路径

我记得的是,这些库必须实际存在于Android项目中才能导出到所产生的“App文件”中。

相关问题