尝试在IntelliJ IDEA(2016.3)中运行Java Quickstart示例Gmail API项目:
https://developers.google.com/gmail/api/quickstart/java
我认为所需的JAR已添加到项目中。
运行项目我得到:
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)
如何摆脱这个错误并运行项目?
JSON文件到位,当我删除它时,我得到"找不到文件" json文件的getGmailService()中的IOException。
Main.authorize(Main.java:98)是
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
Main.getGmailService(Main.java:112)是
Credential credential = authorize();
公共静态Gmail中的getGmailService()会抛出IOException {。
getGmailService()和authorize()定义为:
public static Gmail getGmailService() throws IOException {
Credential credential = authorize();
return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
授权()
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;
}
答案 0 :(得分:1)
您缺少必需的JAR文件,特别是javax.servlet-api
您可以手动下载from mvnrepository,然后您需要add it to your dependencies in IntelliJ IDEA。或者,如果您正在使用Gradle或Maven,请使用mvn repo页面上的设置将依赖项添加到build.gradle文件或pom.xml。
答案 1 :(得分:1)
感谢Trisha,但我设法以不同的方式解决了这个问题 - 我必须通过访问提供的链接和登录gmail帐户来授权访问Google Developers Console中的项目。新凭据现在存储在.credentials目录中,错误消失。