4

google client api(java)を使用して、アプリケーションに対してgoogleappsドメインのユーザーを認証できるかどうか疑問に思っています。ターゲットアプリケーションは、RESTバックエンド(ジャージ)を使用するWebアプリケーションです。

ドキュメントはあまり明確ではありません(または私はそれを誤解しました)、そしてドキュメントのサンプルは非推奨のクラスを参照しています...誰かがそれが可能かどうか、そしてそれを行うための最良の方法を知っていますか?

コードサンプルをいただければ幸いです。

4

1 に答える 1

3

Google Appsアカウントは、APIで正常に機能するはずです。

これに対する唯一の例外は、ドメイン管理者によってサービスが無効にされている場合です。たとえば、ドメイン管理者によってGoogle+機能が無効にされている場合、そのユーザーのGoogle+データにアクセスすることはできません。

コードを変更する必要はないため、クライアントライブラリリポジトリ内の任意のサンプル、またはGoogle+用のこのような製品固有のサンプルのコードを使用できるはずです。

Google+スタータープロジェクトは、最初にOAuthフローを実装しますAbstractAuthorizationCodeServletcom.google.api.sample.OAuth2AuthorizationCodeServlet

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

}
于 2013-01-06T05:04:43.980 に答える