This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch 23041 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 4f7cfd8906cf0aa00c7171d4a47d4f08a731d5ba Author: Andrea Cosentino <[email protected]> AuthorDate: Wed Feb 25 11:27:03 2026 +0100 CAMEL-23041 - Generalize Google services authentication with common module - Google Drive Signed-off-by: Andrea Cosentino <[email protected]> --- components/camel-google/camel-google-drive/pom.xml | 4 ++ .../drive/BatchGoogleDriveClientFactory.java | 55 +++++++--------------- .../google/drive/GoogleDriveConfiguration.java | 10 +++- 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/components/camel-google/camel-google-drive/pom.xml b/components/camel-google/camel-google-drive/pom.xml index 03a3a84f40b5..6b374b46510c 100644 --- a/components/camel-google/camel-google-drive/pom.xml +++ b/components/camel-google/camel-google-drive/pom.xml @@ -40,6 +40,10 @@ </properties> <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-google-common</artifactId> + </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-support</artifactId> diff --git a/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/BatchGoogleDriveClientFactory.java b/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/BatchGoogleDriveClientFactory.java index 6d797e0da132..c7126a7d4541 100644 --- a/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/BatchGoogleDriveClientFactory.java +++ b/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/BatchGoogleDriveClientFactory.java @@ -16,7 +16,6 @@ */ package org.apache.camel.component.google.drive; -import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Proxy; @@ -24,13 +23,12 @@ import java.net.UnknownHostException; import java.util.Collection; import com.google.api.client.auth.oauth2.Credential; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.drive.Drive; import org.apache.camel.CamelContext; import org.apache.camel.RuntimeCamelException; -import org.apache.camel.support.ResourceHelper; +import org.apache.camel.component.google.common.GoogleCredentialsHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,31 +62,21 @@ public class BatchGoogleDriveClientFactory implements GoogleDriveClientFactory { throw new IllegalArgumentException("clientId and clientSecret are required to create Google Drive client."); } try { - Credential credential = authorize(clientId, clientSecret, scopes); + // Use GoogleCredentialsHelper for OAuth credentials + GoogleDriveConfiguration tempConfig = new GoogleDriveConfiguration(); + tempConfig.setClientId(clientId); + tempConfig.setClientSecret(clientSecret); + tempConfig.setRefreshToken(refreshToken); + tempConfig.setAccessToken(accessToken); - if (refreshToken != null && !refreshToken.isEmpty()) { - credential.setRefreshToken(refreshToken); - } - if (accessToken != null && !accessToken.isEmpty()) { - credential.setAccessToken(accessToken); - } + Credential credential + = GoogleCredentialsHelper.getOAuthCredential(null, tempConfig, scopes, transport, jsonFactory); return new Drive.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { throw new RuntimeCamelException("Could not create Google Drive client.", e); } } - // Authorizes the installed application to access user's protected data. - private Credential authorize(String clientId, String clientSecret, Collection<String> scopes) { - // authorize - return new GoogleCredential.Builder() - .setJsonFactory(jsonFactory) - .setTransport(transport) - .setClientSecrets(clientId, clientSecret) - .setServiceAccountScopes(scopes) - .build(); - } - @Override public Drive makeClient( CamelContext camelContext, String serviceAccountKey, Collection<String> scopes, String applicationName, @@ -97,27 +85,16 @@ public class BatchGoogleDriveClientFactory implements GoogleDriveClientFactory { throw new IllegalArgumentException("serviceAccountKey is required to create Drive client."); } try { - Credential credential = authorizeServiceAccount(camelContext, serviceAccountKey, delegate, scopes); + // Use GoogleCredentialsHelper for service account credentials + GoogleDriveConfiguration tempConfig = new GoogleDriveConfiguration(); + tempConfig.setServiceAccountKey(serviceAccountKey); + tempConfig.setDelegate(delegate); + + Credential credential + = GoogleCredentialsHelper.getOAuthCredential(camelContext, tempConfig, scopes, transport, jsonFactory); return new Drive.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { throw new RuntimeCamelException("Could not create Drive client.", e); } } - - private Credential authorizeServiceAccount( - CamelContext camelContext, String serviceAccountKey, String delegate, Collection<String> scopes) { - // authorize - try { - GoogleCredential cred = GoogleCredential - .fromStream(ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, serviceAccountKey), - transport, - jsonFactory) - .createScoped(scopes != null && !scopes.isEmpty() ? scopes : null) - .createDelegated(delegate); - cred.refreshToken(); - return cred; - } catch (IOException e) { - throw new RuntimeException(e); - } - } } diff --git a/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConfiguration.java b/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConfiguration.java index e74a360069f8..214d6572d5ae 100644 --- a/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConfiguration.java +++ b/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConfiguration.java @@ -19,6 +19,7 @@ package org.apache.camel.component.google.drive; import java.util.Collection; import java.util.List; +import org.apache.camel.component.google.common.GoogleCommonConfiguration; import org.apache.camel.component.google.drive.internal.GoogleDriveApiName; import org.apache.camel.spi.Configurer; import org.apache.camel.spi.Metadata; @@ -31,7 +32,7 @@ import org.apache.camel.spi.UriPath; */ @UriParams @Configurer(extended = true) -public class GoogleDriveConfiguration { +public class GoogleDriveConfiguration implements GoogleCommonConfiguration { @UriPath @Metadata(required = true) private GoogleDriveApiName apiName; @@ -78,6 +79,7 @@ public class GoogleDriveConfiguration { this.methodName = methodName; } + @Override public String getClientId() { return clientId; } @@ -89,6 +91,7 @@ public class GoogleDriveConfiguration { this.clientId = clientId; } + @Override public String getClientSecret() { return clientSecret; } @@ -100,6 +103,7 @@ public class GoogleDriveConfiguration { this.clientSecret = clientSecret; } + @Override public String getAccessToken() { return accessToken; } @@ -111,6 +115,7 @@ public class GoogleDriveConfiguration { this.accessToken = accessToken; } + @Override public String getRefreshToken() { return refreshToken; } @@ -138,6 +143,7 @@ public class GoogleDriveConfiguration { return scopes; } + @Override public Collection<String> getScopesAsList() { if (scopes != null) { return List.of(scopes.split(",")); @@ -157,6 +163,7 @@ public class GoogleDriveConfiguration { this.scopes = scopes; } + @Override public String getServiceAccountKey() { return serviceAccountKey; } @@ -171,6 +178,7 @@ public class GoogleDriveConfiguration { this.serviceAccountKey = serviceAccountKey; } + @Override public String getDelegate() { return delegate; }
