@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Create an instance of GoogleOAuthParameters GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); oauthParameters.setOAuthConsumerKey(Constants.CONSUMER_KEY); oauthParameters.setOAuthConsumerSecret(Constants.CONSUMER_SECRET); GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthHmacSha1Signer()); // Remember the token secret that we stashed? Let's get it back // now. We need to add it to oauthParameters String oauthTokenSecret = (String) req.getSession().getAttribute("oauthTokenSecret"); oauthParameters.setOAuthTokenSecret(oauthTokenSecret); // The query string should contain the oauth token, so we can just // pass the query string to our helper object to correctly // parse and add the parameters to our instance of oauthParameters oauthHelper.getOAuthParametersFromCallback(req.getQueryString(), oauthParameters); try { // Now that we have all the OAuth parameters we need, we can // generate an access token and access token secret. These // are the values we want to keep around, as they are // valid for all API calls in the future until a user revokes // our access. String accessToken = oauthHelper.getAccessToken(oauthParameters); String accessTokenSecret = oauthParameters.getOAuthTokenSecret(); PersistenceManager pm = PMF.get().getPersistenceManager(); UserService users = UserServiceFactory.getUserService(); User user = users.getCurrentUser(); if (user != null) { AuthenticatedUser authUser = new AuthenticatedUser(); authUser.setAuthToken(accessToken); authUser.setAuthTokenSecret(accessTokenSecret); authUser.setEmailAddress(user.getEmail()); authUser.setUserId(user.getUserId()); pm.makePersistent(authUser); } resp.sendRedirect("/"); } catch (OAuthException e) { // Something went wrong. Usually, you'll end up here if we have invalid // oauth tokens } }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { UserService userService = UserServiceFactory.getUserService(); RequestDispatcher disp = null; if (userService.isUserLoggedIn()) { User user = userService.getCurrentUser(); PersistenceManager pm = PMF.get(); ContactsToken token = null; try { token = pm.getObjectById(ContactsToken.class, user.getEmail()); String teamName = req.getParameter("teamName"); if (teamName != null) { Team team = TeamDAO.getTeam(teamName); if (team != null) { // Get contacts GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); oauthParameters.setOAuthConsumerKey("anonymous"); oauthParameters.setOAuthConsumerSecret("anonymous"); oauthParameters.setOAuthToken(token.getToken()); oauthParameters.setOAuthTokenSecret(token.getTokenSecret()); OAuthSigner signer = new OAuthHmacSha1Signer(); ContactsService service = new ContactsService("ContactFeedDemo"); List<ContactEntry> result = null; List<String> mails = null; try { service.setOAuthCredentials(oauthParameters, signer); ContactFeed resultFeed = service.getFeed( new URL("https://www.google.com/m8/feeds/contacts/default/full"), ContactFeed.class); result = new ArrayList<ContactEntry>(resultFeed.getEntries()); mails = new ArrayList<String>(); for (ContactEntry ce : result) { for (Email em : ce.getEmailAddresses()) { mails.add(em.getAddress()); } } for (TMember tm : team.getMembers()) { mails.remove(tm.getName()); } } catch (OAuthException e) { e.printStackTrace(); } catch (ServiceException e) { e.printStackTrace(); } // end get contact list req.setAttribute("teamName", teamName); // req.setAttribute("teamMembers", teamMembers); req.setAttribute("contacts", mails); RequestDispatcher rd = req.getRequestDispatcher("/AddMember.jsp"); rd.forward(req, resp); } else { String errText = "team with team name " + teamName + "doesnt exist."; req.setAttribute("err", errText); disp = req.getRequestDispatcher("err.jsp"); disp.forward(req, resp); } } else { String errText = "parametru teamName is null"; req.setAttribute("err", errText); disp = req.getRequestDispatcher("err.jsp"); disp.forward(req, resp); } } catch (JDOObjectNotFoundException e) { String consumerKey = "anonymous"; String consumerSecret = "anonymous"; String scope = "https://www.google.com/m8/feeds"; String callback = "http://vrchlpet-pc.appspot.com/callbackservlet"; GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); oauthParameters.setOAuthConsumerKey(consumerKey); oauthParameters.setOAuthConsumerSecret(consumerSecret); oauthParameters.setScope(scope); oauthParameters.setOAuthCallback(callback); OAuthSigner signer = new OAuthHmacSha1Signer(); GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer); try { oauthHelper.getUnauthorizedRequestToken(oauthParameters); String approvalPageUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters); req.getSession().setAttribute("tokenSecret", oauthParameters.getOAuthTokenSecret()); resp.sendRedirect(approvalPageUrl); return; } catch (OAuthException ee) { ee.printStackTrace(); } } } else { disp = req.getRequestDispatcher("/projectcontrol"); disp.forward(req, resp); } }