【问题标题】:Authenticate Google App users with Google OAuth2 API使用 Google OAuth2 API 对 Google App 用户进行身份验证
【发布时间】:2012-11-20 23:31:37
【问题描述】:

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

documentation 不是很清楚(或者我误解了它),文档中的示例指的是已弃用的类......有人知道这是否可能以及最好的方法。

代码示例将不胜感激。

【问题讨论】:

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


    【解决方案1】:

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

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

    无需更改代码,因此您应该能够使用来自任何samples in the client library repository 或产品特定示例(如this one for Google+)的代码。

    Google+ 入门项目首先通过在 com.google.api.sample.OAuth2AuthorizationCodeServlet 中扩展 AbstractAuthorizationCodeServlet 来实现 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();
        }
    }
    

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

    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(); 
        }
    
    }
    

    【讨论】:

    • 超时怎么样,这个会自动在这个类中处理,还是我需要执行额外的处理以防它失效?
    • 客户端库确实会自动(并且透明地)处理令牌刷新。您无需担心超时或其他可能导致令牌失效的情况。
    • Google 的 OAuth Servlet 代码可以在这里找到:github.com/google/google-oauth-java-client/tree/dev/…
    猜你喜欢
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2015-03-24
    相关资源
    最近更新 更多