Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.i("LOGLEARN", "Starting AlarmReceiver");
- Drive service;
- try {
- HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
- service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
- .setApplicationName("Morning Learning")
- .build();
- FileList result = service.files().list()
- .setPageSize(10)
- .setFields("nextPageToken, files(id, name)")
- .execute();
- List<File> files = result.getFiles();
- if (files == null || files.isEmpty()) {
- Log.i(DEBUG,"No files found.");
- } else {
- System.out.println("Files:");
- for (File file : files) {
- Log.i(DEBUG, "%s (%s) - " + file.getName() + file.getId());
- }
- }
- } catch (Exception exception) {
- // Handle Exception here
- Log.i(DEBUG, "Exception: " + exception.toString());
- }
- }
- private static Credential getCredentials(final HttpTransport HTTP_TRANSPORT) throws IOException {
- String DEBUG = "LOGLEARN";
- // Load client secrets.
- InputStream in = AlarmReceiver.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
- if (in == null) {
- throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
- }
- GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
- // Build flow and trigger user authorization request.
- java.io.File tokenFolder = new java.io.File(Environment.getDataDirectory() +
- java.io.File.separator + TOKENS_DIRECTORY_PATH);
- if (!tokenFolder.exists()) {
- Log.i(DEBUG, "Token Folder does not exist " + tokenFolder.toString());
- tokenFolder.mkdirs();
- }
- if (!tokenFolder.exists()) {
- Log.i(DEBUG, "Token Folder still does not exist " + tokenFolder.toString());
- } else {
- Log.i(DEBUG, "Token Folder now exists " + tokenFolder.toString());
- }
- GoogleAuthorizationCodeFlow.Builder build = new GoogleAuthorizationCodeFlow.Builder(
- HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES);
- Log.i(DEBUG, "Checkpoint 1");
- build.setDataStoreFactory(new FileDataStoreFactory(tokenFolder));
- Log.i(DEBUG, "Checkpoint 2");
- build.setAccessType("offline");
- Log.i(DEBUG, "Checkpoint 3");
- GoogleAuthorizationCodeFlow flow = build.build();
- Log.i(DEBUG, "Checkpoint end");
- LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
- return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
- }
- I/LOGLEARN: Starting AlarmReceiver
- I/LOGLEARN: Token Folder does not exist /data/tokens
- I/LOGLEARN: Token Folder still does not exist /data/tokens
- I/LOGLEARN: Checkpoint 1
- I/LOGLEARN: Exception: java.io.IOException: unable to create directory: /data/tokens
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement