语境:的oauth2 OAuthServiceFactory.getOAuthService()getCurrentUser()崩溃
我有一个球衣bachend服务器上的应用程序引擎运行的应用程序angularJs。到目前为止,我使用(更简单的)openId APi来认证我的用户,但现在看来这将被删除,oAuth2是“新”事物。
问:
我有下面的代码使用apacha oltu到我的用户进行身份验证:
@GET
public Response step1()
throws ServletException, IOException, URISyntaxException, OAuthSystemException {
OAuthClientRequest request = OAuthClientRequest
.authorizationProvider(OAuthProviderType.GOOGLE)
.setClientId(CLIENT_ID)
.setScope("https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email")
.setRedirectURI(REDIRECT_URI)
.setResponseType("code")
.buildQueryMessage();
logger.info("start oauth process: " + request.getLocationUri());
//We send a redirect request to the browser so it can go to the consent screen of google.
return Response.status(Response.Status.TEMPORARY_REDIRECT).location(new URI(request.getLocationUri())).build();
}
@GET
@Path("loggedin")
public Response getCode(@QueryParam("code") String code)
throws ServletException, IOException, URISyntaxException, OAuthSystemException, OAuthProblemException, OAuthRequestException {
logger.info("response from redirect. We obtained the following code: " + code);
OAuthClientRequest request = OAuthClientRequest
.tokenProvider(OAuthProviderType.GOOGLE)
.setGrantType(GrantType.AUTHORIZATION_CODE)
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.setCode(code)
.setScope("https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email")
.setRedirectURI(REDIRECT_URI)
.buildBodyMessage();
OAuthClient client = new OAuthClient(new URLConnectionClient());
OAuthJSONAccessTokenResponse tokenResponse = client.accessToken(request, OAuthJSONAccessTokenResponse.class);
logger.log(Level.WARNING, "token: " + tokenResponse.getAccessToken() + " valid to: " + tokenResponse.getExpiresIn());
return Response.status(Response.Status.TEMPORARY_REDIRECT).location(new URI("../index.html")).build();
}
现在的问题是,OAuthServiceFactory.getOAuthService().getCurrentUser()
崩溃,并InvalidOAuthParametersException。
我在这里找到了一个类似的问题在stackoverflow但解决方案不适用/不适合我。 oauth进程本身似乎是按顺序的:我得到一个访问令牌和一个正确的有效期。 (我假设如果在这个过程中出现问题,我会从oltu获得一个空/空的标记,而不是看起来像一个标记字符串的字符串)。
此前我使用UserService来访问底层用户对象。但它似乎只适用于openId。
我的oltu方法的替代方法是使用谷歌+登录的东西。 我不这样做的原因是我只找到一个例子,这使用了一些JavaScript的东西 - 我想严格地在服务器上处理所有oauth的东西,不包括任何其他js库。
他们是替代访问某种用户对象的“ID”和“是用户登录”? (如果用户未登录,则返回UserService.getCurrentUser()返回null)。