public void handleAuthorizationCodeResult(String resultUrl, String redirectUrl)
     throws IOException {
   AuthorizationCodeResponseUrl responseUrl = new AuthorizationCodeResponseUrl(resultUrl);
   String code = responseUrl.getCode();
   if (responseUrl.getError() != null || code == null) {
     throw new IllegalArgumentException("Error response received.");
   } else {
     TokenResponse response = flow.newTokenRequest(code).setRedirectUri(redirectUrl).execute();
     Credential credential = flow.createAndStoreCredential(response, userId);
     System.out.println(credential);
   }
 }
  protected final void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    StringBuffer buf = req.getRequestURL();
    if (req.getQueryString() != null) {
      buf.append('?').append(req.getQueryString());
    }

    AuthorizationCodeResponseUrl responseUrl = new AuthorizationCodeResponseUrl(buf.toString());

    String code = responseUrl.getCode();
    if (responseUrl.getError() != null) {
      String state = URLDecoder.decode(req.getParameter("state"), "UTF-8");
      String[] params = state.split("\\|");
      resp.sendRedirect(params[1]);
    } else if (code == null) {
      resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      resp.getWriter().print("Missing authorization code");
    } else {
      String redirectUri = ServletUtils.getRedirectUri(req);
      lock.lock();
      try {
        if (flow == null) {
          flow = ServletUtils.newFlow();
        }
        TokenResponse response = flow.newTokenRequest(code).setRedirectUri(redirectUri).execute();

        HttpSession sess = req.getSession(true);
        Credential credential = flow.createAndStoreCredential(response, sess.getId());
        String[] params = URLDecoder.decode(req.getParameter("state"), "UTF-8").split("\\|");
        String client = params[2];
        String email = ServletUtils.getEmail(credential);
        UserEntity user = User.get(client, email);
        if (user != null) {
          sess.setAttribute("login", email);
          sess.setAttribute("loginType", "google");
        }

        resp.sendRedirect(params[0]);
      } finally {
        lock.unlock();
      }
    }
  }