簡體   English   中英

使用Google OAuth2 API對Google App用戶進行身份驗證

[英]Authenticate Google App users with Google OAuth2 API

我想知道我是否可以使用谷歌客戶端API(Java)來驗證谷歌應用程序域的用戶到我的應用程序。 目標應用程序是使用REST后端(jersey)的Web應用程序。

文檔不是很清楚(或者我誤解了),文檔中的示例引用了已棄用的類...有人知道它是否可能並且是最好的方法。

代碼示例將不勝感激。

Google Apps帳戶應該可以與API一起使用。

唯一的例外是域管理員禁用該服務。 例如,如果域管理員禁用了Google+功能,您將無法訪問該用戶的Google+數據。

不需要更改代碼,因此您應該能夠使用客戶端庫存儲庫中的任何示例中的代碼或像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(); 
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM