从Google OAuth登录后,页面刷新无效

时间:2014-10-09 11:08:57

标签: java spring oauth google-oauth

我已经使用Google OAuth和java进行身份验证。当我登录时,我会重定向到一个页面,我会得到这样的http://www.demo.com/demo/PROJECTNAME/oauthRedirect?state=google;294822862&code=4/aUEAqsMu7o074wsJymyAK9CMseWR.YlkPPRXJzt8Vcp7tdiljKKbHfgXpkQI 但是当我尝试刷新页面时,它给了我错误

 com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "invalid_grant",
  "error_description" : "Invalid code."
}
com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:103)
com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:303)
com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:175)
com.mob.googleoauth.GoogleAuthHelper.getUserInfoJson(GoogleAuthHelper.java:108)
com.mob.controller.UserController.user(UserController.java:55)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

1 个答案:

答案 0 :(得分:0)

在Google上成功验证后,它会向您发送一个代码( 4 / aUEAqsMu7o074wsJymyAK9CMseWR.YlkPPRXJzt8Vcp7tdiljKKbHfgXpkQI )。再次使用此代码,您可以使用POST请求访问Google API。然后Google会向您发送 access_token 。使用此access_token再次调用Google API,它将返回所需的数据。