Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. import com.google.api.client.auth.oauth2.Credential;
  2. import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
  3. import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
  4. import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
  5. import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
  6. import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
  7. import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
  8. import com.google.api.client.http.HttpTransport;
  9. import com.google.api.client.http.javanet.NetHttpTransport;
  10. import com.google.api.client.json.JsonFactory;
  11. import com.google.api.client.json.jackson2.JacksonFactory;
  12. import com.google.api.client.util.store.FileDataStoreFactory;
  13. import com.google.api.services.sheets.v4.Sheets;
  14. import com.google.api.services.sheets.v4.SheetsScopes;
  15. import com.google.api.services.sheets.v4.model.ValueRange;
  16.  
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.InputStreamReader;
  20. import java.security.GeneralSecurityException;
  21. import java.util.Collections;
  22. import java.util.List;
  23.  
  24. public class SheetsQuickstart {
  25. private static final String APPLICATION_NAME = "Google Sheets API Java Quickstart";
  26. private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
  27. private static final String TOKENS_DIRECTORY_PATH = "tokens";
  28.  
  29. /**
  30. * Global instance of the scopes required by this quickstart.
  31. * If modifying these scopes, delete your previously saved tokens/ folder.
  32. */
  33. private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS_READONLY);
  34. private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
  35.  
  36. /**
  37. * Creates an authorized Credential object.
  38. * @param HTTP_TRANSPORT The network HTTP Transport.
  39. * @return An authorized Credential object.
  40. * @throws IOException If the credentials.json file cannot be found.
  41. */
  42. private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
  43. // Load client secrets.
  44. InputStream in = SheetsQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
  45. GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
  46.  
  47. // Build flow and trigger user authorization request.
  48. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
  49. HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
  50. .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
  51. .setAccessType("offline")
  52. .build();
  53. LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
  54. return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
  55. }
  56.  
  57.  
  58.  
  59. public static Sheets createSheetsService() throws IOException, GeneralSecurityException {
  60. HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
  61. JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
  62.  
  63. // TODO: Change placeholder below to generate authentication credentials. See
  64. // https://developers.google.com/sheets/quickstart/java#step_3_set_up_the_sample
  65. //
  66. // Authorize using one of the following scopes:
  67. // "https://www.googleapis.com/auth/drive"
  68. // "https://www.googleapis.com/auth/drive.file"
  69. // "https://www.googleapis.com/auth/spreadsheets"
  70. GoogleCredential credential = null;
  71.  
  72. return new Sheets.Builder(httpTransport, jsonFactory, credential)
  73. .setApplicationName("Google-SheetsSample/0.1")
  74. .build();
  75. }
  76. /**
  77. * Prints the names and majors of students in a sample spreadsheet:
  78. * https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
  79. */
  80. public static void main(String... args) throws IOException, GeneralSecurityException {
  81. // Build a new authorized API client service.
  82. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
  83. final String spreadsheetId = "1Cz66gpzisC8O8J53LsJk_p0Rp8fQEK6I3Q1S2-6K4Ys";
  84. final String range = "A2:E";
  85.  
  86.  
  87. Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
  88. .setApplicationName(APPLICATION_NAME)
  89. .build();
  90. ValueRange response = service.spreadsheets().values()
  91. .get(spreadsheetId, range)
  92. .execute();
  93.  
  94.  
  95. List<List<Object>> values = response.getValues();
  96. if (values == null || values.isEmpty()) {
  97. System.out.println("No data found.");
  98. } else {
  99. System.out.println("Name, Major");
  100. for (List row : values) {
  101. // Print columns A and E, which correspond to indices 0 and 4.
  102. System.out.printf("%s, %s\n", row.get(0), row.get(1));
  103. }
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement