-2

Here's my code.

My program to upload files to 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());
  }
}

Getting error as:

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)

Can any one help me?

Christian St.
  • 1,751
  • 2
  • 22
  • 41
jai
  • 1
  • 1

1 Answers1

1

Assuming you get the error while executing your app and you work with Eclipse, this should solve your problem:

  1. remove your 3rd-party libraries from the build path
  2. create a folder in your Android project called libs
  3. copy your 3rd-party libraries into the new created folder
  4. add the copied libraries to the build path

What I remember is, that the libraries must be physically present in an Android project to be exported to the resulting "App file".

Christian St.
  • 1,751
  • 2
  • 22
  • 41