Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. package net.codejava.servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import ruTherePackage.ruThere;
  11.  
  12. public class Login extends HttpServlet {
  13.  
  14. public void doPost(HttpServletRequest request, HttpServletResponse response)
  15. throws IOException, ServletException {
  16. String email = request.getParameter("email");
  17. PrintWriter write = response.getWriter();
  18.  
  19. ruThere.updateSheets();
  20. write.println("<html>Success</html>");
  21. }
  22. }
  23.  
  24.  
  25. package ruTherePackage;
  26. // This is ruThere. This program is used as a demo to demostrate an update to a fix cell location on a Google sheet
  27. // Todos: Please be sure to update the location of your client_secret.json file & the GoogleSheets id before running your program.
  28. // Date: 6/1/18
  29. import com.google.api.client.auth.oauth2.Credential;
  30. import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
  31. import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
  32. import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
  33. import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
  34. import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
  35. import com.google.api.client.http.HttpTransport;
  36. import com.google.api.client.json.jackson2.JacksonFactory;
  37. import com.google.api.client.json.JsonFactory;
  38. import com.google.api.client.util.store.FileDataStoreFactory;
  39. import com.google.api.services.sheets.v4.SheetsScopes;
  40. import com.google.api.services.sheets.v4.model.*;
  41. import com.google.api.services.sheets.v4.Sheets;
  42. import com.google.api.services.sheets.v4.model.ValueRange;
  43.  
  44. import java.io.FileInputStream;
  45. import java.io.FileReader;
  46. import java.io.File;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.InputStreamReader;
  50. import java.text.SimpleDateFormat;
  51. import java.util.ArrayList;
  52. import java.util.Arrays;
  53. import java.util.List;
  54. import java.util.Scanner;
  55. import java.util.Date;
  56. import java.util.Random;
  57. import java.util.Iterator;
  58. import java.util.Map;
  59.  
  60. import org.json.simple.JSONObject;
  61. import org.json.simple.parser.*;
  62.  
  63. public class ruThere {
  64.  
  65. /** Application name. */
  66. private static final String APPLICATION_NAME = "ruThere";
  67.  
  68. /** Directory to store user credentials for this application. */
  69. private static final java.io.File DATA_STORE_DIR = new java.io.File(
  70. System.getProperty("user.home"), "credentials/sheets.googleapis.com-java-quickstart.json");
  71.  
  72. /** Global instance of the {@link FileDataStoreFactory}. */
  73. private static FileDataStoreFactory DATA_STORE_FACTORY;
  74.  
  75. /** Global instance of the JSON factory. */
  76. private static final JsonFactory JSON_FACTORY =
  77. JacksonFactory.getDefaultInstance();
  78.  
  79. /** Global instance of the HTTP transport. */
  80. private static HttpTransport HTTP_TRANSPORT;
  81.  
  82. /** Global instance of the scopes required by this quickstart.
  83. *
  84. * If modifying these scopes, delete your previously saved credentials
  85. * at ~/.credentials/sheets.googleapis.com-java-quickstart.json
  86. */
  87. private static final List<String> SCOPES =
  88. Arrays.asList( SheetsScopes.SPREADSHEETS );
  89.  
  90. static {
  91. try {
  92. HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
  93. DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
  94. } catch (Throwable t) {
  95. t.printStackTrace();
  96. System.exit(1);
  97. }
  98. }
  99.  
  100. public static Credential authorize() throws IOException {
  101. // Load client secrets.
  102. // Todo: Change this text to the location where your client_secret.json resided
  103. InputStream in = new FileInputStream(System.getProperty("user.home") + "/IdeaProjects/ruThere/src/main/resources/client_secret.json");
  104. // ruThere.class.getResourceAsStream("/client_secret.json");
  105. GoogleClientSecrets clientSecrets =
  106. GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
  107.  
  108. // Build flow and trigger user authorization request.
  109. GoogleAuthorizationCodeFlow flow =
  110. new GoogleAuthorizationCodeFlow.Builder(
  111. HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
  112. .setDataStoreFactory(DATA_STORE_FACTORY)
  113. .setAccessType("offline")
  114. .build();
  115. Credential credential = new AuthorizationCodeInstalledApp(
  116. flow, new LocalServerReceiver()).authorize("user");
  117. System.out.println(
  118. "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
  119. return credential;
  120. }
  121.  
  122. public static Sheets getSheetsService() throws IOException {
  123. Credential credential = authorize();
  124. return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
  125. .setApplicationName(APPLICATION_NAME)
  126. .build();
  127. }
  128.  
  129. public static String getSpreadsheetId(Scanner kb, Sheets service) throws IOException{
  130. while(true) {
  131. try {
  132. System.out.print("Enter your spreadsheet ID--> ");
  133. String spreadsheetId = kb.nextLine();
  134. service.spreadsheets().values().get(spreadsheetId, "F1:F2");
  135. return spreadsheetId;
  136. } catch (com.google.api.client.googleapis.json.GoogleJsonResponseException e) {
  137. System.out.println("Invalid Sheet ID");
  138. }
  139. }
  140. }
  141. public static void main(String[] args) throws IOException {
  142. //public static void updateSheets() throws IOException {
  143. JSONObject professorInfo = (JSONObject) getEmailInfo("username@gmail.com");
  144. String spreadsheetId = (String) professorInfo.get("sheetId");
  145. Sheets service = getSheetsService();
  146. GoogleSheets mySheet = new GoogleSheets(spreadsheetId, service);
  147. mySheet.generateKeyFor("sheet1");
  148. System.out.println("done");
  149. }
  150.  
  151. public static File getFile(String fileName) {
  152. return new File(ruThere.class.getClassLoader().getResource(fileName).getFile());
  153. }
  154.  
  155. public static Object getEmailInfo(String email){
  156. try {
  157. JSONObject database = (JSONObject) new JSONParser().parse(
  158. new FileReader(
  159. getFile("database.json")));
  160.  
  161. Map emails = ((Map)database.get("emails"));
  162. Iterator<Map.Entry> iterator = emails.entrySet().iterator();
  163. while (iterator.hasNext()) {
  164. Map.Entry pair = iterator.next();
  165. if(pair.getKey().equals(email)) {
  166. return pair.getValue();
  167. }
  168. }
  169. return null;
  170. } catch (Exception e) {
  171. return null;
  172. }
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement