Search in sources :

Example 6 with User

use of com.google.api.services.drive.model.User in project components by Talend.

the class GoogleDriveDataSource method validate.

@Override
public ValidationResult validate(RuntimeContainer container) {
    ValidationResult vr = new ValidationResultMutable(Result.OK);
    // check for Connection attributes
    if (StringUtils.isEmpty(serviceAccountFile)) {
        vr = new ValidationResultMutable(Result.ERROR).setMessage("Service Account JSON File cannot be empty.");
        return vr;
    }
    try {
        // make a dummy call to check drive's connection..
        User u = getDriveService().about().get().setFields("user").execute().getUser();
        LOG.debug("[validate] Testing User Properties: {}.", u);
    } catch (Exception ex) {
        LOG.error("[validate] {}.", ex.getMessage());
        vr = new ValidationResultMutable(Result.ERROR).setMessage(ex.getMessage());
        return vr;
    }
    return ValidationResult.OK;
}
Also used : User(com.google.api.services.drive.model.User) ValidationResultMutable(org.talend.daikon.properties.ValidationResultMutable) ValidationResult(org.talend.daikon.properties.ValidationResult) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 7 with User

use of com.google.api.services.drive.model.User in project components by Talend.

the class GoogleDriveRuntime method validateConnection.

public ValidationResult validateConnection(GoogleDriveConnectionProperties connectionProperties) {
    if ((OAuthMethod.InstalledApplicationWithIdAndSecret.equals(connectionProperties.oAuthMethod.getValue()) || OAuthMethod.InstalledApplicationWithJSON.equals(connectionProperties.oAuthMethod.getValue())) && container == null) {
        cleanupCredentialsStore(getDatastoreFile(connectionProperties).toPath().resolve("StoredCredential"));
    }
    ValidationResultMutable vr = new ValidationResultMutable(validateConnectionProperties(connectionProperties));
    if (Result.ERROR.equals(vr.getStatus())) {
        return vr;
    }
    try {
        // make a dummy call to check drive's connection..
        User u = getDriveService().about().get().setFields("user").execute().getUser();
        LOG.debug("[validateConnection] Testing User Properties: {}.", u);
    } catch (Exception ex) {
        LOG.error("[validateConnection] {}.", ex.getMessage());
        vr.setStatus(Result.ERROR);
        vr.setMessage(ex.getMessage());
        return vr;
    }
    vr.setStatus(Result.OK);
    vr.setMessage(messages.getMessage("message.connectionSuccessful"));
    return vr;
}
Also used : User(com.google.api.services.drive.model.User) ValidationResultMutable(org.talend.daikon.properties.ValidationResultMutable) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 8 with User

use of com.google.api.services.drive.model.User in project components by Talend.

the class GoogleDriveDataSourceTest method testValidate.

@Test
public void testValidate() throws Exception {
    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("test@example.org");
    about.setUser(user);
    when(drive.about().get().setFields(anyString()).execute()).thenReturn(about);
    assertEquals(Result.OK, dataSource.validate(container).getStatus());
}
Also used : User(com.google.api.services.drive.model.User) Drive(com.google.api.services.drive.Drive) GoogleDriveUtils(org.talend.components.google.drive.runtime.GoogleDriveUtils) About(com.google.api.services.drive.model.About) Test(org.junit.Test)

Example 9 with User

use of com.google.api.services.drive.model.User in project OpenRefine by OpenRefine.

the class GDataImportingController method listSpreadsheets.

private void listSpreadsheets(Drive drive, JsonGenerator writer) throws IOException {
    com.google.api.services.drive.Drive.Files.List files = drive.files().list();
    files.setQ("mimeType = 'application/vnd.google-apps.spreadsheet'");
    files.setFields("nextPageToken, files(id, name, webViewLink, owners, modifiedTime)");
    FileList fileList = files.execute();
    for (File entry : fileList.getFiles()) {
        writer.writeStartObject();
        writer.writeStringField("docId", entry.getId());
        writer.writeStringField("docLink", entry.getWebViewLink());
        writer.writeStringField("docSelfLink", entry.getWebViewLink());
        writer.writeStringField("title", entry.getName());
        writer.writeStringField("type", "spreadsheet");
        com.google.api.client.util.DateTime updated = entry.getModifiedTime();
        if (updated != null) {
            writer.writeStringField("updated", updated.toString());
        }
        writer.writeArrayFieldStart("authors");
        for (User user : entry.getOwners()) {
            writer.writeString(user.getDisplayName());
        }
        writer.writeEndArray();
        writer.writeEndObject();
    }
}
Also used : User(com.google.api.services.drive.model.User) FileList(com.google.api.services.drive.model.FileList) File(com.google.api.services.drive.model.File)

Aggregations

User (com.google.api.services.drive.model.User)9 IOException (java.io.IOException)6 File (com.google.api.services.drive.model.File)5 Permission (com.google.api.services.drive.model.Permission)4 GeneralSecurityException (java.security.GeneralSecurityException)4 ArrayList (java.util.ArrayList)4 FileContent (com.google.api.client.http.FileContent)2 About (com.google.api.services.drive.model.About)2 ParentReference (com.google.api.services.drive.model.ParentReference)2 ParseException (java.text.ParseException)2 Test (org.junit.Test)2 ValidationResultMutable (org.talend.daikon.properties.ValidationResultMutable)2 Drive (com.google.api.services.drive.Drive)1 FileList (com.google.api.services.drive.model.FileList)1 GoogleDriveUtils (org.talend.components.google.drive.runtime.GoogleDriveUtils)1 ValidationResult (org.talend.daikon.properties.ValidationResult)1