Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. package UploadToDrive;
  2.  
  3.  
  4.  
  5. import com.google.api.client.auth.oauth2.Credential;
  6. import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
  7. import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
  8. import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
  9. import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
  10. import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
  11. import com.google.api.client.http.HttpTransport;
  12. import com.google.api.client.json.jackson2.JacksonFactory;
  13. import com.google.api.client.json.JsonFactory;
  14. import com.google.api.client.util.store.FileDataStoreFactory;
  15.  
  16. import com.google.api.services.drive.DriveScopes;
  17. import com.google.api.services.drive.model.*;
  18. import com.google.api.services.drive.Drive;
  19.  
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.InputStreamReader;
  23. import java.util.Arrays;
  24. import java.util.List;
  25.  
  26. public class DriveQuickstart {
  27. /** Application name. */
  28. private static final String APPLICATION_NAME =
  29. "Drive API Quickstart";
  30.  
  31. /** Directory to store user credentials for this application. */
  32. private static final java.io.File DATA_STORE_DIR = new java.io.File(
  33. System.getProperty("user.home"), ".credentials/drive-java-quickstart.json");
  34.  
  35. /** Global instance of the {@link FileDataStoreFactory}. */
  36. private static FileDataStoreFactory DATA_STORE_FACTORY;
  37.  
  38. /** Global instance of the JSON factory. */
  39. private static final JsonFactory JSON_FACTORY =
  40. JacksonFactory.getDefaultInstance();
  41.  
  42. /** Global instance of the HTTP transport. */
  43. private static HttpTransport HTTP_TRANSPORT;
  44.  
  45. /** Global instance of the scopes required by this quickstart.
  46. *
  47. * If modifying these scopes, delete your previously saved credentials
  48. * at ~/.credentials/drive-java-quickstart.json
  49. */
  50. private static final List<String> SCOPES =
  51. Arrays.asList(DriveScopes.DRIVE_METADATA_READONLY);
  52.  
  53. static {
  54. try {
  55. HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
  56. DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
  57. } catch (Throwable t) {
  58. t.printStackTrace();
  59. System.exit(1);
  60. }
  61. }
  62.  
  63. /**
  64. * Creates an authorized Credential object.
  65. * @return an authorized Credential object.
  66. * @throws IOException
  67. */
  68. public static Credential authorize() throws IOException {
  69. // Load client secrets.
  70. InputStream in =
  71. DriveQuickstart.class.getResourceAsStream("C:\TESTINGS\build\classes\main\resources\client_secret.json");
  72. GoogleClientSecrets clientSecrets =
  73. GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
  74.  
  75. // Build flow and trigger user authorization request.
  76. GoogleAuthorizationCodeFlow flow =
  77. new GoogleAuthorizationCodeFlow.Builder(
  78. HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
  79. .setDataStoreFactory(DATA_STORE_FACTORY)
  80. .setAccessType("offline")
  81. .build();
  82. Credential credential = new AuthorizationCodeInstalledApp(
  83. flow, new LocalServerReceiver()).authorize("user");
  84. System.out.println(
  85. "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
  86. return credential;
  87. }
  88.  
  89. /**
  90. * Build and return an authorized Drive client service.
  91. * @return an authorized Drive client service
  92. * @throws IOException
  93. */
  94. public static Drive getDriveService() throws IOException {
  95. Credential credential = authorize();
  96. return new Drive.Builder(
  97. HTTP_TRANSPORT, JSON_FACTORY, credential)
  98. .setApplicationName(APPLICATION_NAME)
  99. .build();
  100. }
  101.  
  102. public static void main(String[] args) throws IOException {
  103. // Build a new authorized API client service.
  104. Drive service = getDriveService();
  105.  
  106. // Print the names and IDs for up to 10 files.
  107. FileList result = service.files().list()
  108. .setPageSize(10)
  109. .setFields("nextPageToken, files(id, name)")
  110. .execute();
  111. List<File> files = result.getFiles();
  112. if (files == null || files.size() == 0) {
  113. System.out.println("No files found.");
  114. } else {
  115. System.out.println("Files:");
  116. for (File file : files) {
  117. System.out.printf("%s (%s)n", file.getName(), file.getId());
  118. }
  119. }
  120. }
  121.  
  122. }
  123.  
  124. Jul 25, 2016 3:52:03 PM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
  125. WARNING: unable to change permissions for everybody: C:UsersSilverFear.credentialsdrive-java-quickstart.json
  126. Jul 25, 2016 3:52:03 PM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
  127. WARNING: unable to change permissions for owner: C:UsersSilverFear.credentialsdrive-java-quickstart.json
  128. Exception in thread "main" java.lang.NullPointerException
  129. at java.io.Reader.<init>(Reader.java:78)
  130. at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
  131. at UploadToDrive.DriveQuickstart.authorize(DriveQuickstart.java:77)
  132. at UploadToDrive.DriveQuickstart.getDriveService(DriveQuickstart.java:100)
  133. at UploadToDrive.DriveQuickstart.main(DriveQuickstart.java:109)
  134. C:UsersSilverFearAppDataLocalNetBeansCache8.1executor-snippetsrun.xml:53: Java returned: 1
  135. BUILD FAILED (total time: 0 seconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement