0

I am trying to run the tutorial google drive api test

package Tests;
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.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;

public class DriveQuickstart {
    private static final String APPLICATION_NAME = "Google Drive API Java Quickstart";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens";

    /**
     * Global instance of the scopes required by this quickstart.
     * If modifying these scopes, delete your previously saved tokens/ folder.
     */
    private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
    private static final String CREDENTIALS_FILE_PATH = "credentials.json";

    /**
     * Creates an authorized Credential object.
     * @param HTTP_TRANSPORT The network HTTP Transport.
     * @return An authorized Credential object.
     * @throws IOException If the credentials.json file cannot be found.
     */
    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStream in = DriveQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        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(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }

    public static void main(String... args) throws IOException, GeneralSecurityException {
        // Build a new authorized API client service.
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();

        // 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.isEmpty()) {
            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());
            }
        }
    }
}

I have almost all jars mentioned and I have downloaded the config.json file. However I get the following error

WARNING: unable to change permissions for owner: C:\Users\aswat\git\bionovaQAnew\tokens
Exception in thread "main" java.lang.NoClassDefFoundError: org/mortbay/jetty/Handler
    at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver$Builder.build(LocalServerReceiver.java:205)
    at Tests.DriveQuickstart.getCredentials(DriveQuickstart.java:57)
    at Tests.DriveQuickstart.main(DriveQuickstart.java:64)
Caused by: java.lang.ClassNotFoundException: org.mortbay.jetty.Handler
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 3 more

I have tried out multiple answers mentioned in stackoverflow.com/questions/50687434/error-running-spark-main-class-exception-in-thread-thread-0-java-lang-noclas, but it doesnt help me. I have attached my buildpath dependencies as well. Please let me know what the issue is. enter image description here

I use Ant and eclipse.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
aswathy
  • 139
  • 2
  • 16
  • I find it hard to understand that that code would result in that error message. The code is just listing files while the error message says that you are trying to change permissions. Are you sure this is the code you are using that returns that error? – Linda Lawton - DaImTo Aug 18 '20 at 07:00
  • @DaImTo, Yes this is the only code I am running – aswathy Aug 18 '20 at 09:47
  • https://stackoverflow.com/q/30634827/1841839 <-- this might help – Linda Lawton - DaImTo Aug 19 '20 at 06:44
  • Does this answer your question? [WARNING: unable to change permissions for everybody:](https://stackoverflow.com/questions/30634827/warning-unable-to-change-permissions-for-everybody) – Raserhin Aug 19 '20 at 07:55
  • No, tried multiple answers in both the links, but didnt work – aswathy Aug 25 '20 at 09:35

1 Answers1

0

After many trials what worked for me was:

  1. Added guava-11.0.2.jar
  2. Removed google api client json 1 2 3 alpha.jar
  3. Re downloaded the credential.json
  4. Started a new project with just the required jars in resource file as shown attached

enter image description here

aswathy
  • 139
  • 2
  • 16