Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. @Override
  2. public void onReceive(Context context, Intent intent) {
  3.  
  4. Log.i("LOGLEARN", "Starting AlarmReceiver");
  5. Drive service;
  6. try {
  7. HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
  8. service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
  9. .setApplicationName("Morning Learning")
  10. .build();
  11.  
  12. FileList result = service.files().list()
  13. .setPageSize(10)
  14. .setFields("nextPageToken, files(id, name)")
  15. .execute();
  16. List<File> files = result.getFiles();
  17. if (files == null || files.isEmpty()) {
  18. Log.i(DEBUG,"No files found.");
  19. } else {
  20. System.out.println("Files:");
  21. for (File file : files) {
  22. Log.i(DEBUG, "%s (%s) - " + file.getName() + file.getId());
  23. }
  24. }
  25. } catch (Exception exception) {
  26. // Handle Exception here
  27. Log.i(DEBUG, "Exception: " + exception.toString());
  28. }
  29. }
  30.  
  31. private static Credential getCredentials(final HttpTransport HTTP_TRANSPORT) throws IOException {
  32. String DEBUG = "LOGLEARN";
  33. // Load client secrets.
  34. InputStream in = AlarmReceiver.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
  35. if (in == null) {
  36. throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
  37. }
  38. GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
  39.  
  40. // Build flow and trigger user authorization request.
  41. java.io.File tokenFolder = new java.io.File(Environment.getDataDirectory() +
  42. java.io.File.separator + TOKENS_DIRECTORY_PATH);
  43. if (!tokenFolder.exists()) {
  44. Log.i(DEBUG, "Token Folder does not exist " + tokenFolder.toString());
  45. tokenFolder.mkdirs();
  46. }
  47.  
  48. if (!tokenFolder.exists()) {
  49. Log.i(DEBUG, "Token Folder still does not exist " + tokenFolder.toString());
  50. } else {
  51. Log.i(DEBUG, "Token Folder now exists " + tokenFolder.toString());
  52. }
  53.  
  54. GoogleAuthorizationCodeFlow.Builder build = new GoogleAuthorizationCodeFlow.Builder(
  55. HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES);
  56. Log.i(DEBUG, "Checkpoint 1");
  57.  
  58. build.setDataStoreFactory(new FileDataStoreFactory(tokenFolder));
  59.  
  60. Log.i(DEBUG, "Checkpoint 2");
  61. build.setAccessType("offline");
  62.  
  63. Log.i(DEBUG, "Checkpoint 3");
  64. GoogleAuthorizationCodeFlow flow = build.build();
  65.  
  66. Log.i(DEBUG, "Checkpoint end");
  67.  
  68. LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
  69. return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
  70. }
  71.  
  72. I/LOGLEARN: Starting AlarmReceiver
  73. I/LOGLEARN: Token Folder does not exist /data/tokens
  74. I/LOGLEARN: Token Folder still does not exist /data/tokens
  75. I/LOGLEARN: Checkpoint 1
  76. I/LOGLEARN: Exception: java.io.IOException: unable to create directory: /data/tokens
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement