【发布时间】: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