我正在开发一个Spring-MVC应用程序,我正在尝试集成日历功能。不幸的是,由于json错误,我陷入了身份验证阶段。我正在尝试使用过时的Googles示例代码,因此我做了一些修改。请看看。 错误日志:
java.lang.NoSuchMethodError: com.google.api.client.json.JsonFactory.fromInputStream(Ljava/io/InputStream;Ljava/lang/Class;)Ljava/lang/Object;
com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.load(GoogleClientSecrets.java:167)
com.journaldev.spring.utility.OAuth.authorize(OAuth.java:73)
Oauth类,我实现了日历功能。
public class OAuth {
private static final String APPLICATION_NAME = "appname";
private static final java.io.File DATA_STORE_DIR =
new java.io.File(System.getProperty("user.home"), "" +
"/Store/calendar_sample");
private static FileDataStoreFactory dataStoreFactory;
private static HttpTransport httpTransport;
private static final com.google.api.client.json.JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static com.google.api.services.calendar.Calendar client;
static final java.util.List<Calendar> addedCalendarsUsingBatch = Lists.newArrayList();
public void authorizeAndTest(){
try {
httpTransport = new NetHttpTransport();
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
Credential credential = authorize();
client = new com.google.api.services.calendar.Calendar.Builder(httpTransport,JSON_FACTORY,credential).setApplicationName(APPLICATION_NAME).build();
showCalenders();
addCalendarsUsingBatch();
Calendar calendar = addCalendar();
updateCalendar(calendar);
addEvent(calendar);
showEvents(calendar);
deleteCalendarInBatch();
deleteCalendar(calendar);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Credential authorize() throws Exception{
InputStream inputStream = new FileInputStream(new File("/home/akshay/c0cc3b4b7502.json"));
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,inputStream);
//发生在这条线上。
if (clientSecrets.getDetails().getClientId().startsWith("Enter")
|| clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println(
"Enter Client ID and Secret from https://code.google.com/apis/console/?api=calendar "
+ "into calendar-cmdline-sample/src/main/resources/client_secrets.json");
return null;
}
AuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport,JSON_FACTORY,clientSecrets,
Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(dataStoreFactory).build();
return new AuthorizationCodeInstalledApp(flow,new LocalServerReceiver()).authorize("user@gmail.com");
}
的pom.xml
<dependencies>
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client-json</artifactId>
<version>1.2.3-alpha</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>1.19.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-calendar</artifactId>
<version>v3-rev35-1.13.2-beta</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-java6</artifactId>
<version>1.19.0</version>
</dependency>
<dependency>
<groupId>com.google.gdata</groupId>
<artifactId>core</artifactId>
<version>1.47.1</version>
</dependency>
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client-auth-oauth2</artifactId>
<version>1.2.3-alpha</version>
</dependency>
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.4.1-beta</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson</artifactId>
<version>1.19.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
David要求的错误日志:
java.lang.IllegalArgumentException
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:76)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:37)
at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.getDetails(GoogleClientSecrets.java:82)
at com.journaldev.spring.utility.OAuth.authorize(OAuth.java:75)
at com.journaldev.spring.utility.OAuth.authorizeAndTest(OAuth.java:53)
at com.journaldev.spring.controller.PersonController.listCanvas(PersonController.java:586)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.filters.ExpiresFilter.doFilter(ExpiresFilter.java:1179)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
答案 0 :(得分:1)
只需在Pom.xml中注释以下依赖项,然后尝试:
<!--dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client-auth-oauth2</artifactId>
<version>1.2.3-alpha</version>
</dependency-->
不是这个:
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.4.1-beta</version>
</dependency>
希望这可以帮到你。
答案 1 :(得分:1)
您需要考虑以下几点:
Calendar API不需要依赖项com.google.gdata.core
。除非您需要其他Google API,否则可将其删除
您有google-http-client
的两个实现:google-http-client-jackson
和google-http-client-jackson2
。你需要删除其中一个。
您正在使用不必要且过时的依赖关系,例如google-api-client-auth-oauth2:1.2.3-alpha
和google-api-client:1.4.1-beta
google-api-services-calendar
和google-http-client-jackson2
的版本不匹配。对于google-http-client-jackson2:1.19.0
,您需要使用google-api-services-calendar:v3-rev121-1.19.0
。请注意,HTTP客户端的版本与日历客户端版本的后缀相同?
如果您想使用google-oauth-client-java6
类,google-oauth-client-jetty
依赖关系必须替换为LocalServerReceiver
。
最后,这是我的工作依赖集和略微更新的OAuth
类:
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-calendar</artifactId>
<version>v3-rev121-1.19.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson</artifactId>
<version>1.19.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.19.0</version>
</dependency>
请注意,您需要更改json文件的路径:
public class OAuth {
private static final String APPLICATION_NAME = "APPNAME";
private static final java.io.File DATA_STORE_DIR =
new java.io.File(System.getProperty("user.home"), "" +
"/Store/calendar_sample");
private static FileDataStoreFactory dataStoreFactory;
private static HttpTransport httpTransport;
private static final com.google.api.client.json.JsonFactory JSON_FACTORY = new JacksonFactory();
private static com.google.api.services.calendar.Calendar client;
public void authorizeAndTest() {
try {
httpTransport = new NetHttpTransport();
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
Credential credential = authorize();
} catch (Exception e) {
e.printStackTrace();
}
}
private static Credential authorize() throws Exception {
FileReader credentialsReader = new FileReader(new File("client_secret_47576556653-go0fkf4c56dohm6m16s2400qed5ps410.apps.googleusercontent.com.json"));
// It gives me an error for line below.
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, credentialsReader);
if (clientSecrets.getDetails().getClientId().startsWith("Enter")
|| clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println(
"Enter Client ID and Secret from https://code.google.com/apis/console/?api=calendar "
+ "into calendar-cmdline-sample/src/main/resources/client_secrets.json");
return null;
}
AuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets,
Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(dataStoreFactory).build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user@gmail.com");
}