使用Google OAuth2 API对Google App用户进行身份验证

时间:2012-12-03 13:30:15

标签: java rest google-api oauth-2.0 google-apps

我想知道我是否可以使用google客户端api(java)向我的应用程序验证谷歌应用程序域的用户。 目标应用程序是使用REST后端(jersey)的Web应用程序。

documentation不是很清楚(或者我误解了),文档中的示例涉及不推荐的类...有人知道它是否可行且是最好的方式去做吧。

代码示例将不胜感激。

1 个答案:

答案 0 :(得分:3)

Google Apps帐户应该可以正常使用API​​。

唯一的例外是域管理员禁用该服务。例如,如果域管理员停用了Google+功能,您将无法访问该用户的Google+数据。

无需更改代码,因此您应该能够使用samples in the client library repository中的任何代码或this one for Google+等产品特定样本中的代码。

Google+初学者项目首先通过在AbstractAuthorizationCodeServlet

中扩展com.google.api.sample.OAuth2AuthorizationCodeServlet来实施OAuth流程
public class OAuth2AuthorizationCodeServlet 
    extends AbstractAuthorizationCodeServlet {
    /**
     * If the user already has a valid credential held in the 
     * AuthorizationCodeFlow they are simply returned to the home page.
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
        response.sendRedirect("/");
    }

    /**
     * Returns the URI to redirect to with the authentication result.
     */
    @Override
    protected String getRedirectUri(HttpServletRequest request)
                    throws ServletException, IOException {
        return ConfigHelper.REDIRECT_URI;
    }

    /**
     * Returns the HTTP session id as the identifier for the current user.  
     * The users credentials are stored against this ID.
     */
    @Override
    protected String getUserId(HttpServletRequest request)
                    throws ServletException, IOException {
        return request.getSession(true).getId();
    }

    @Override
    protected AuthorizationCodeFlow initializeFlow() throws ServletException,
                    IOException {
        return Util.getFlow();
    }
}

然后通过扩展com.google.api.sample.Oauth2CallbackServlet完成AbstractAuthorizationCodeCallbackServlet中的流程:

public class OAuth2CallbackServlet 
    extends AbstractAuthorizationCodeCallbackServlet {    
    @Override
    protected void onSuccess(HttpServletRequest request, 
            HttpServletResponse response, Credential credential)
            throws ServletException, IOException {
        response.sendRedirect("/");
    }

    @Override
    protected void onError(HttpServletRequest req, HttpServletResponse resp, 
            AuthorizationCodeResponseUrl errorResponse)
            throws ServletException, IOException {
        resp.sendError(SC_INTERNAL_SERVER_ERROR, "Something went wrong :(");
    }

    @Override
    protected String getRedirectUri(HttpServletRequest request) 
            throws ServletException, IOException {
        return ConfigHelper.REDIRECT_URI;
    }

    @Override
    protected AuthorizationCodeFlow initializeFlow() 
            throws IOException {
        return Util.getFlow();
    }

    @Override
    protected String getUserId(HttpServletRequest request) throws ServletException, IOException {
        return  request.getSession(true).getId(); 
    }

}