com.google.api.services.drive.model.About Java Examples

The following examples show how to use com.google.api.services.drive.model.About. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DriveSession.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void login(final Proxy proxy, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
    authorizationService.setTokens(authorizationService.authorize(host, prompt, cancel));
    final About about;
    try {
        about = client.about().get().setFields("user").execute();
    }
    catch(IOException e) {
        throw new DriveExceptionMappingService().map(e);
    }
    if(log.isDebugEnabled()) {
        log.debug(String.format("Authenticated as user %s", about.getUser()));
    }
    final Credentials credentials = host.getCredentials();
    credentials.setUsername(about.getUser().getEmailAddress());
}
 
Example #2
Source File: DriveQuotaFeature.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Space get() throws BackgroundException {
    // Team Drives should not have any Quota applied.
    if (new DriveHomeFinderService(session).find().isChild(DriveHomeFinderService.TEAM_DRIVES_NAME)) {
        return new Space(0L, Long.MAX_VALUE);
    }

    try {
        final About about = session.getClient().about().get().setFields("user, storageQuota").execute();
        final Long used = null == about.getStorageQuota().getUsageInDrive() ? 0L
            : about.getStorageQuota().getUsageInDrive();
        final Long available = (null == about.getStorageQuota().getLimit() ? Long.MAX_VALUE
            : about.getStorageQuota().getLimit()) - used;
        return new Space(used, available);
    }
    catch(IOException e) {
        throw new DriveExceptionMappingService().map("Failure to read attributes of {0}", e,
            new Path(String.valueOf(Path.DELIMITER), EnumSet.of(Path.Type.volume, Path.Type.directory)));
    }
}
 
Example #3
Source File: GoogleDriveDataSourceTest.java    From components with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidate() throws Exception {
    inputProperties.getDatasetProperties().getDatastoreProperties().serviceAccountJSONFile.setValue("");
    dataSource.initialize(container, inputProperties);
    assertEquals(Result.ERROR, dataSource.validate(container).getStatus());

    dataSource = spy(dataSource);
    Drive drive = mock(Drive.class, RETURNS_DEEP_STUBS);
    GoogleDriveUtils utils = mock(GoogleDriveUtils.class, RETURNS_DEEP_STUBS);
    doReturn(drive).when(dataSource).getDriveService();
    doReturn(utils).when(dataSource).getDriveUtils();
    inputProperties.getDatasetProperties().getDatastoreProperties().serviceAccountJSONFile.setValue("service.json");
    dataSource.initialize(container, inputProperties);

    About about = new About();
    User user = new User();
    user.setEmailAddress("[email protected]");
    about.setUser(user);
    when(drive.about().get().setFields(anyString()).execute()).thenReturn(about);
    assertEquals(Result.OK, dataSource.validate(container).getStatus());
}
 
Example #4
Source File: GoogleDriveServiceImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public boolean token(String userId, String code) {
	if (!isConfigured()) {
		return false;
	}

	try {
		GoogleTokenResponse googleResponse = flow.newTokenRequest(code)
				.setRedirectUri(redirectUri)
				.execute();
		Credential cred = flow.createAndStoreCredential(googleResponse, userId);

		service = new Drive.Builder(httpTransport, jsonFactory, cred)
			.setApplicationName(GOOGLEDRIVE_APP_NAME)
			.build();

		Drive.About about = service.about();
		Drive.About.Get get = about.get().setFields("user(displayName, emailAddress, permissionId)");
		About ab = get.execute();			
		log.debug("About : {}", ab.toString());
		
		GoogleDriveUser du = getGoogleDriveUser(userId);
		du.setGoogleDriveUserId(ab.getUser().getPermissionId());
		du.setGoogleDriveName(ab.getUser().getEmailAddress());
		googledriveRepo.update(du);
		return true;

	} catch(Exception e) {
		log.warn("GoogleDrive: Error while retrieving or saving the credentials for user {} : {}", userId, e.getMessage());
		revokeGoogleDriveConfiguration(userId);
	}
	return false;
}
 
Example #5
Source File: GoogleDriveRuntimeTest.java    From components with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidateConnection() throws Exception {
    About about = new About();
    User user = new User();
    user.setEmailAddress("[email protected]");
    about.setUser(user);
    when(drive.about().get().setFields(anyString()).execute()).thenReturn(about);
    assertEquals(Result.OK, testRuntime.validateConnection(testRuntime.getConnectionProperties()).getStatus());
}
 
Example #6
Source File: GoogleDriveServiceImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public boolean token(String userId, String code) {
	if (!isConfigured()) {
		return false;
	}

	try {
		GoogleTokenResponse googleResponse = flow.newTokenRequest(code)
				.setRedirectUri(redirectUri)
				.execute();
		Credential cred = flow.createAndStoreCredential(googleResponse, userId);

		service = new Drive.Builder(httpTransport, jsonFactory, cred)
			.setApplicationName(GOOGLEDRIVE_APP_NAME)
			.build();

		Drive.About about = service.about();
		Drive.About.Get get = about.get().setFields("user(displayName, emailAddress, permissionId)");
		About ab = get.execute();			
		log.debug("About : {}", ab.toString());
		
		GoogleDriveUser du = getGoogleDriveUser(userId);
		du.setGoogleDriveUserId(ab.getUser().getPermissionId());
		du.setGoogleDriveName(ab.getUser().getEmailAddress());
		googledriveRepo.update(du);
		return true;

	} catch(Exception e) {
		log.warn("GoogleDrive: Error while retrieving or saving the credentials for user {} : {}", userId, e.getMessage());
		revokeGoogleDriveConfiguration(userId);
	}
	return false;
}
 
Example #7
Source File: SyncAdapter.java    From mytracks with Apache License 2.0 4 votes vote down vote up
/**
 * Performs initial sync.
 */
private void performInitialSync() throws IOException {

  // Get the largest change id first to avoid race conditions
  About about = drive.about().get().setFields(ABOUT_GET_FIELDS).execute();
  long largestChangeId = about.getLargestChangeId();

  // Get all the KML/KMZ files in the "My Drive:/My Tracks" folder
  Files.List myTracksFolderRequest = drive.files()
      .list().setQ(String.format(Locale.US, SyncUtils.MY_TRACKS_FOLDER_FILES_QUERY, folderId));
  Map<String, File> myTracksFolderMap = getFiles(myTracksFolderRequest, true);

  // Handle tracks that are already uploaded to Google Drive
  Set<String> syncedDriveIds = updateSyncedTracks();
  for (String driveId : syncedDriveIds) {
    myTracksFolderMap.remove(driveId);
  }

  // Get all the KML/KMZ files in the "Shared with me:/" folder
  Files.List sharedWithMeRequest = drive.files()
      .list().setQ(SyncUtils.SHARED_WITH_ME_FILES_QUERY);
  Map<String, File> sharedWithMeMap = getFiles(sharedWithMeRequest, false);

  try {
    insertNewTracks(myTracksFolderMap.values());
    insertNewTracks(sharedWithMeMap.values());
    PreferencesUtils.setLong(context, R.string.drive_largest_change_id_key, largestChangeId);
  } catch (IOException e) {

    // Remove all imported tracks
    Cursor cursor = null;
    try {
      cursor = myTracksProviderUtils.getTrackCursor(SyncUtils.DRIVE_ID_TRACKS_QUERY, null, null);
      if (cursor != null && cursor.moveToFirst()) {
        do {
          Track track = myTracksProviderUtils.createTrack(cursor);
          if (!syncedDriveIds.contains(track.getDriveId())) {
            myTracksProviderUtils.deleteTrack(context, track.getId());
          }
        } while (cursor.moveToNext());
      }
    } finally {
      if (cursor != null) {
        cursor.close();
      }
    }
    throw e;
  }
}