1

Trying to run Java Quickstart sample Gmail API project in IntelliJ IDEA (2016.3):

https://developers.google.com/gmail/api/quickstart/java

I think that required JARs are added to the project.

Running the project I get:

Apr 12, 2017 8:27:34 AM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
WARNING: unable to change permissions for everybody: C:\Users\xxx\.credentials\some-project-name
Apr 12, 2017 8:27:34 AM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
WARNING: unable to change permissions for owner: C:\Users\xxx\.credentials\some-project-name
Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
    at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:98)
    at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:76)
    at Main.authorize(Main.java:98)
    at Main.getGmailService(Main.java:112)
    at Main.main(Main.java:123)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

How to get rid of this error and run the project?

JSON file is in place, when I delete it, i get "file not found" IOException in getGmailService() for json file.

Main.authorize(Main.java:98) is

        Credential credential = new AuthorizationCodeInstalledApp(
            flow, new LocalServerReceiver()).authorize("user");

Main.getGmailService(Main.java:112) is

        Credential credential = authorize();

in public static Gmail getGmailService() throws IOException {.

getGmailService() and authorize() are defined as:

    public static Gmail getGmailService() throws IOException {
    Credential credential = authorize();
    return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
}

authorize()

    public static Credential authorize() throws IOException {
    // Load client secrets.
    //System.err.println("Main.class.getResourceAsStream("/"): "+Main.class.getResourceAsStream("/")); // debug
    //InputStream in = Main.class.getResourceAsStream("/client_secret.json");
    File jsonFile = new File("client_secret.json");
    InputStream in = new FileInputStream(jsonFile);
    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;
}
hobdev
  • 33
  • 1
  • 10

2 Answers2

1

You're missing a required JAR file, specifically javax.servlet-api

You can download it manually from mvnrepository, then you'll need to add it to your dependencies in IntelliJ IDEA. Alternatively, if you're using Gradle or Maven, use the settings on the mvn repo page to add the dependency to your build.gradle file or pom.xml.

Community
  • 1
  • 1
Trisha
  • 3,891
  • 1
  • 25
  • 39
1

Thanks Trisha, but I managed to solve it differently - I had to authorize access for my project in the Google Developers Console by visiting provided link and logging into gmail account. New credentials are now stored in .credentials directory and error is gone.

hobdev
  • 33
  • 1
  • 10