Guest User

Untitled

a guest
Nov 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. <dependency>
  2. <groupId>com.google.api-client</groupId>
  3. <artifactId>google-api-client</artifactId>
  4. <version>1.23.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.google.oauth-client</groupId>
  8. <artifactId>google-oauth-client-jetty</artifactId>
  9. <version>1.23.0</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.google.apis</groupId>
  13. <artifactId>google-api-services-drive</artifactId>
  14. <version>v2-rev282-1.23.0</version>
  15. </dependency>
  16.  
  17. import com.google.api.client.auth.oauth2.Credential;
  18. import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
  19. import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
  20. import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
  21. import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
  22. import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
  23. import com.google.api.client.http.HttpTransport;
  24. import com.google.api.client.json.JsonFactory;
  25. import com.google.api.client.json.jackson2.JacksonFactory;
  26. import com.google.api.client.util.store.FileDataStoreFactory;
  27. import com.google.api.services.drive.Drive;
  28. import com.google.api.services.drive.DriveScopes;
  29. import java.io.FileReader;
  30. import java.io.IOException;
  31. import java.io.InputStreamReader;
  32. import java.security.GeneralSecurityException;
  33. import java.util.Arrays;
  34. import java.util.List;
  35.  
  36. /**
  37. *
  38. * @author jalal-sordo
  39. */
  40. public class GoogleDriveServiceFactory {
  41.  
  42. private String applicationName;
  43.  
  44. private JsonFactory jsonFactory;
  45. private HttpTransport googleDriveHttpTransport;
  46. private List<String> googleDriveApiScopes;
  47. private String googleDriveClientSecretFilePath;
  48. private FileDataStoreFactory credentialsStoreFolder;
  49.  
  50. public GoogleDriveServiceFactory(ApplicationParameters params) {
  51. try {
  52. this.googleDriveClientSecretFilePath = params.getGoogleDriveClientSecretFilePath();
  53. // = new FileInputStream(googleDriveClientSecretFilePath);
  54. applicationName = "FileTXTExtractor";
  55. jsonFactory = JacksonFactory.getDefaultInstance();
  56. googleDriveApiScopes = Arrays.asList(DriveScopes.DRIVE);
  57. googleDriveHttpTransport = GoogleNetHttpTransport.newTrustedTransport();
  58. java.io.File googleDriveCredentialsStore = new java.io.File(params.getGoogleDriveCredentialsFolderPath());
  59. credentialsStoreFolder = new FileDataStoreFactory(googleDriveCredentialsStore);
  60. } catch (IOException | GeneralSecurityException t) {
  61. System.err.println(t.getMessage());
  62. System.exit(1);
  63. }
  64. }
  65.  
  66. public Credential authorize() throws IOException {
  67. InputStreamReader streamReader = new FileReader(new java.io.File(googleDriveClientSecretFilePath));
  68. GoogleClientSecrets clientSecrets
  69. = GoogleClientSecrets.load(jsonFactory, streamReader);
  70. // Build flow and trigger user authorization request.
  71. GoogleAuthorizationCodeFlow flow
  72. = new GoogleAuthorizationCodeFlow.Builder(
  73. googleDriveHttpTransport, jsonFactory, clientSecrets, googleDriveApiScopes)
  74. .setDataStoreFactory(credentialsStoreFolder)
  75. .setAccessType("offline")
  76. .build();
  77. Credential credential = new AuthorizationCodeInstalledApp(
  78. flow, new LocalServerReceiver()).authorize("user");
  79.  
  80. return credential;
  81. }
  82.  
  83. public Drive getDriveService() throws IOException {
  84. Credential credential = authorize();
  85. return new Drive.Builder(
  86. googleDriveHttpTransport, jsonFactory, credential)
  87. .setApplicationName(applicationName)
  88. .build();
  89. }
  90. }
  91.  
  92. /**
  93. *
  94. * @author jalal-sordo
  95. */
  96. public class ApplicationParameters {
  97.  
  98. private String googleDriveCredentialsFolderPath;
  99. private String googleDriveClientSecretFilePath;
  100.  
  101. public String getGoogleDriveCredentialsFolderPath() {
  102. return googleDriveCredentialsFolderPath;
  103. }
  104.  
  105. public void setGoogleDriveCredentialsFolderPath(String googleDriveCredentialsFolderPath) {
  106. this.googleDriveCredentialsFolderPath = googleDriveCredentialsFolderPath;
  107. }
  108.  
  109. public String getGoogleDriveClientSecretFilePath() {
  110. return googleDriveClientSecretFilePath;
  111. }
  112.  
  113. public void setGoogleDriveClientSecretFilePath(String googleDriveClientSecretFilePath) {
  114. this.googleDriveClientSecretFilePath = googleDriveClientSecretFilePath;
  115. }
  116.  
  117. @Override
  118. public String toString() {
  119. return "ApplicationParameters{"
  120. + "ngoogleDriveCredentialsFolderPath = " + googleDriveCredentialsFolderPath
  121. + "ngoogleDriveClientSecretFilePath = " + googleDriveClientSecretFilePath
  122. + "n}";
  123. }
  124.  
  125. }
  126.  
  127. ApplicationParameters params = new ApplicationParameters();
  128. params.setGoogleDriveCredentialsFolderPath("...");//this is where you specify where you want the credentils to be stored
  129. params.setGoogleDriveClientSecretFilePath(clientSecret.get());//this is the path to your client_id that you download from google cloud console credentials page.
  130. GoogleDriveServiceFactory driveFactory = new GoogleDriveServiceFactory (params);
  131.  
  132. File fileMetadata = new File();
  133. fileMetadata.setName("photo.jpg");
  134. java.io.File filePath = new java.io.File("files/photo.jpg");
  135. FileContent mediaContent = new FileContent("image/jpeg", filePath);
  136. File file = driveFactory.getDriveService.files().create(fileMetadata, mediaContent)
  137. .setFields("id")
  138. .execute();
  139. System.out.println("File ID: " + file.getId());
Add Comment
Please, Sign In to add comment