无法使用Java客户端与Gmail API集成

时间:2018-03-22 21:16:40

标签: java spring google-oauth gmail-api

我试图根据他们的官方教程与gmail集成 https://developers.google.com/gmail/api/quickstart/java

我几乎通过复制粘贴实现了他们的课程

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;
        }

    }

}

我已经创建了凭据文件,如图所示,当我运行它时,我收到以下错误

  

java.lang.NoClassDefFoundError:org / mortbay / component / LifeCycle       在java.base / java.lang.ClassLoader.defineClass1(本机方法)

即时通讯在我的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>

知道出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

我刚刚将这个依赖项添加到我的项目

 for rows in df['FedFY']:
    if rows = df['FedFY'].date_range(10/1/2016 1:00, 10/1/2017 1:00):
        then df['FedFY'] =  df['FedFY'].replace({rows : "2017"})
    elif df['FedFY'] =  df['FedFY'].replace({rows : "2018"})

它开始工作了。 它用于启动jetty以打开oAuth的授权窗口