@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter writer = response.getWriter(); writer.println("<!doctype html><html><head>"); writer.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">"); writer.println("<title>" + APP_NAME + "</title>"); writer.println("</head><body>"); final Credential v1credential = this.getCredential(); HttpRequestFactory requestFactory = Utils.HTTP_TRANSPORT.createRequestFactory( new HttpRequestInitializer() { public void initialize(HttpRequest request) throws IOException { v1credential.initialize(request); } }); GenericUrl v1url = new GenericUrl(secrets.getServerBaseUri()); // Add the OAuth API end-point v1url.getPathParts().add("rest-1.oauth.v1"); // Add a simple data query for the currently logged in member v1url.getPathParts().add("Data"); v1url.getPathParts().add("Member"); v1url.set("where", "IsSelf=\'true\'"); // Send request to VersionOne and print the results. HttpRequest v1request = requestFactory.buildGetRequest(v1url); HttpResponse v1response = v1request.execute(); printMemberDetails(writer, v1response); writer.println("</body></html>"); }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { // If something went wrong, log the error message. if (req.getParameter("error") != null) { LOG.severe("Something went wrong during auth: " + req.getParameter("error")); res.setContentType("text/plain"); res.getWriter().write("Something went wrong during auth. Please check your log for details"); return; } // If we have a code, finish the OAuth 2.0 dance if (req.getParameter("code") != null) { LOG.info("Got a code. Attempting to exchange for access token."); AuthorizationCodeFlow flow = AuthUtil.newAuthorizationCodeFlow(); TokenResponse tokenResponse = flow.newTokenRequest(req.getParameter("code")) .setRedirectUri(WebUtil.buildUrl(req, "/oauth2callback")) .execute(); // Extract the Google User ID from the ID token in the auth response String userId = ((GoogleTokenResponse) tokenResponse).parseIdToken().getPayload().getUserId(); LOG.info("Code exchange worked. User " + userId + " logged in."); // Set it into the session AuthUtil.setUserId(req, userId); flow.createAndStoreCredential(tokenResponse, userId); // TODO: Store it into ListableMemoryCrednetialStore // AuthUtil.storeUserIdCredential(userId, credential); // The dance is done. Do our bootstrapping stuff for this user Credential credential = AuthUtil.newAuthorizationCodeFlow().loadCredential(userId); Contacts.insert(req, userId, credential); Subscriber.insert(req, userId, credential); // Redirect back to get started res.sendRedirect(WebUtil.buildUrl(req, "/get_started.jsp")); // TEST - PLEASE DELETE IT // MovieInfoCard.insert(req, credential); // TicketPurchaseCard.insert(req, credential); // DirectionCard.insert(req, credential); return; } // Else, we have a new flow. Initiate a new flow. LOG.info("No auth context found. Kicking off a new auth flow."); AuthorizationCodeFlow flow = AuthUtil.newAuthorizationCodeFlow(); GenericUrl url = flow.newAuthorizationUrl().setRedirectUri(WebUtil.buildUrl(req, "/oauth2callback")); url.set("approval_prompt", "force"); res.sendRedirect(url.build()); }