protected GoogleTokenResponse obtainAccessToken(HttpServletRequest request) throws IOException {
HttpSession session = request.getSession();
String stateFromSession = (String)session.getAttribute(OAuthConstants.ATTRIBUTE_VERIFICATION_STATE);
String stateFromRequest = request.getParameter(OAuthConstants.STATE_PARAMETER);
if (stateFromSession == null || stateFromRequest == null || !stateFromSession.equals(stateFromRequest)) {
throw new OAuthException(OAuthExceptionCode.INVALID_STATE, "Validation of state parameter failed. stateFromSession="
+ stateFromSession + ", stateFromRequest=" + stateFromRequest);
}
// Check if user didn't permit scope
String error = request.getParameter(OAuthConstants.ERROR_PARAMETER);
if (error != null) {
if (OAuthConstants.ERROR_ACCESS_DENIED.equals(error)) {
throw new OAuthException(OAuthExceptionCode.USER_DENIED_SCOPE, error);
} else {
throw new OAuthException(OAuthExceptionCode.UNKNOWN_ERROR, error);
}
} else {
String code = request.getParameter(OAuthConstants.CODE_PARAMETER);
GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(TRANSPORT, JSON_FACTORY, clientID,