【问题标题】:Cannot integrate with Gmail API using Java client无法使用 Java 客户端与 Gmail API 集成
【发布时间】:2018-09-01 11:30:00
【问题描述】:

我正在尝试根据他们的 [官方教程] 与 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(Native Method)

我在我的 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>


知道出了什么问题吗?

【问题讨论】:

    标签: java spring google-oauth gmail-api


    【解决方案1】:

    我刚刚将此依赖项添加到我的项目中:

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

    它开始工作了。

    用于启动Jetty,打开OAuth的授权窗口。

    【讨论】:

      猜你喜欢
      • 2018-02-07
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-26
      • 2022-07-26
      • 2018-02-25
      相关资源
      最近更新 更多