Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. import com.google.api.client.auth.oauth2.Credential;
  2. import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
  3. import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
  4. import com.google.api.client.http.HttpTransport;
  5. import com.google.api.client.json.JsonFactory;
  6. import com.google.api.client.json.jackson2.JacksonFactory;
  7. import com.google.api.client.util.store.FileDataStoreFactory;
  8. import com.google.api.services.drive.Drive;
  9. import com.google.api.services.drive.DriveScopes;
  10. import com.google.api.services.drive.model.File;
  11. import com.google.api.services.drive.model.FileList;
  12. import com.google.api.services.drive.model.Permission;
  13. import com.google.api.services.drive.model.PermissionList;
  14. import com.google.common.io.Resources;
  15.  
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.Arrays;
  19. import java.util.List;
  20.  
  21. public class Quickstart {
  22. /**
  23. * Application name.
  24. */
  25. private static final String APPLICATION_NAME =
  26. "Drive API Java Quickstart";
  27.  
  28. /**
  29. * Directory to store user credentials for this application.
  30. */
  31. private static final java.io.File DATA_STORE_DIR = new java.io.File(
  32. System.getProperty("user.home"), ".credentials/drive-java-quickstart");
  33.  
  34. /**
  35. * Global instance of the {@link FileDataStoreFactory}.
  36. */
  37. private static FileDataStoreFactory DATA_STORE_FACTORY;
  38.  
  39. /**
  40. * Global instance of the JSON factory.
  41. */
  42. private static final JsonFactory JSON_FACTORY =
  43. JacksonFactory.getDefaultInstance();
  44.  
  45. /**
  46. * Global instance of the HTTP transport.
  47. */
  48. private static HttpTransport HTTP_TRANSPORT;
  49.  
  50. /**
  51. * Global instance of the scopes required by this quickstart.
  52. * <p>
  53. * If modifying these scopes, delete your previously saved credentials
  54. * at ~/.credentials/drive-java-quickstart
  55. */
  56. private static final List<String> SCOPES =
  57. Arrays.asList(DriveScopes.DRIVE_FILE);
  58.  
  59. static {
  60. try {
  61. HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
  62. DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
  63. } catch (Throwable t) {
  64. t.printStackTrace();
  65. System.exit(1);
  66. }
  67. }
  68.  
  69. /**
  70. * Creates an authorized Credential object.
  71. *
  72. * @return an authorized Credential object.
  73. * @throws IOException
  74. */
  75. private static Credential authorize() throws IOException {
  76. // Build flow and trigger user authorization request.
  77. try (InputStream inputStream = Resources.getResource("sa-key-file.json").openStream()) {
  78. return GoogleCredential.fromStream(inputStream).createScoped(SCOPES);
  79. }
  80. }
  81.  
  82. /**
  83. * Build and return an authorized Drive client service.
  84. *
  85. * @return an authorized Drive client service
  86. * @throws IOException
  87. */
  88. private static Drive getDriveService() throws IOException {
  89. Credential credential = authorize();
  90. return new Drive.Builder(
  91. HTTP_TRANSPORT, JSON_FACTORY, credential)
  92. .setApplicationName(APPLICATION_NAME)
  93. .build();
  94. }
  95.  
  96. public static void main(String[] args) throws IOException {
  97. // Build a new authorized API client service.
  98. Drive service = getDriveService();
  99.  
  100.  
  101. //service.files().create(new File().setName("Hello_empt").setMimeType("application/pdf")).execute();
  102.  
  103. Permission execute = service.permissions()
  104. .create("1gDCeEBQhQS_omfYA49sRlxtKoPlSn0PV", new Permission()
  105. .setType("user")
  106. .setEmailAddress("aodocs.storage.1@dev.revevolcloud.com")
  107. //.setDomain("dev.revevolcloud.com")
  108. .setRole("writer"))
  109. .setFields("*")
  110. .execute();
  111.  
  112. System.out.println("Added permission: " + execute);
  113.  
  114. PermissionList permissionList = service.permissions().list("1gDCeEBQhQS_omfYA49sRlxtKoPlSn0PV").execute();
  115.  
  116. permissionList.getPermissions().forEach(permission -> System.out.println(permission));
  117.  
  118. // Print the names and IDs for up to 10 files.
  119. FileList result = service.files().list()
  120. .setPageSize(10)
  121. .setFields("nextPageToken, files(id, name)")
  122. .execute();
  123. List<File> files = result.getFiles();
  124. if (files == null || files.size() == 0) {
  125. System.out.println("No files found.");
  126. } else {
  127. System.out.println("Files:");
  128. for (File file : files) {
  129. System.out.printf("%s (%s)\n", file.getName(), file.getId());
  130. }
  131. }
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement