I'm wondering if I can use the google client api (java) to authenticate the users of a google apps domain to my application.
The target application is a web application using a REST backend (jersey).
The documentation isn't very clear (or I misunderstood it), and the samples in the documentation refers to deprecated classes... Does someone knows if it's possible and the best way to do it.
A code sample would be appreciate.
Google Apps accounts should work fine with the APIs.
The only exception to this is if the service is disabled by the domain administrator. For example, if the Google+ feature is disabled by the domain administrator, you're not going to be able to access that user's Google+ data.
No code change is necessary, so you should be able to use the code from any of the samples in the client library repository or the product specific samples like this one for Google+.
The Google+ starter project implements the OAuth flow first by extending AbstractAuthorizationCodeServlet
in com.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();
}
}
And then by completing the flow in com.google.api.sample.Oauth2CallbackServlet
by extending 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();
}
}