naoru
naoru

Reputation: 2227

Cannot integrate with Gmail API using Java client

I'm trying to integrate with Gmail based on their [official tutorial] (https://developers.google.com/gmail/api/quickstart/java).

I've implemented their class almost by copy paste:

public class GmailServiceImplV2 {

    private static final String APPLICATION_NAME = "gdax-bot-v3";

    /** Directory to store user credentials for this application. */
    private static final java.io.File DATA_STORE_DIR = new java.io.File("/gdaxbot/");

    /** 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/gmail-java-quickstart
     */
    private static final List<String> SCOPES = Arrays.asList(GmailScopes.GMAIL_LABELS);

    static {
        try {
            HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
        } catch (Throwable t) {
            System.out.println(t);
            System.exit(1);
        }
    }

    public static Credential authorize() throws IOException {
        // Load client secrets.
        InputStream in = GmailServiceImplV2.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();
        try {
            Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("me");
            System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
            return credential;
        } catch(Exception ex) {
            System.out.println(ex);
            return null;
        }

    }

}

I've created the credentials file as shown yet when I run it I get the following error:

java.lang.NoClassDefFoundError: org/mortbay/component/LifeCycle at java.base/java.lang.ClassLoader.defineClass1(Native Method)

I'm using the following libraries in my POM:

<dependency>
    <groupId>com.google.api-client</groupId>
    <artifactId>google-api-client</artifactId>
    <version>1.23.0</version>
</dependency>

<dependency>
    <groupId>com.google.oauth-client</groupId>
    <artifactId>google-oauth-client-jetty</artifactId>
    <version>1.23.0</version>
</dependency>

<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-gmail</artifactId>
    <version>v1-rev82-1.23.0</version>
</dependency>


Any idea what went wrong?

Upvotes: 1

Views: 234

Answers (1)

naoru
naoru

Reputation: 2227

I just added this dependency to my project:

<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-util</artifactId>
    <version>6.1.25</version>
</dependency>

and it started to work.

It's used to launch Jetty to open the authorization window for OAuth.

Upvotes: 1

Related Questions