From c58f79f5d44b12f5961eb978fb79ccfd1958f3da Mon Sep 17 00:00:00 2001 From: itsankit-google Date: Fri, 9 Feb 2024 16:00:02 +0000 Subject: [PATCH] Add scopes when service account json/filePath is provided --- .../plugin/google/common/GoogleDriveClient.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/cdap/plugin/google/common/GoogleDriveClient.java b/src/main/java/io/cdap/plugin/google/common/GoogleDriveClient.java index 1dee4c8..50bb204 100644 --- a/src/main/java/io/cdap/plugin/google/common/GoogleDriveClient.java +++ b/src/main/java/io/cdap/plugin/google/common/GoogleDriveClient.java @@ -32,6 +32,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; @@ -93,23 +94,25 @@ protected Credential getOAuth2Credential() { .setClientSecrets(config.getClientId(), config.getClientSecret()) .build(); - credential.setRefreshToken(config.getRefreshToken()); + credential.createScoped(getRequiredScopes()).setRefreshToken(config.getRefreshToken()); return credential; } protected HttpCredentialsAdapter getServiceAccountCredential() throws IOException { GoogleCredentials googleCredentials; + List scopes = getRequiredScopes(); if (Boolean.TRUE.equals(config.isServiceAccountJson())) { InputStream jsonInputStream = new ByteArrayInputStream( - config.getServiceAccountJson().getBytes()); - googleCredentials = GoogleCredentials.fromStream(jsonInputStream); + config.getServiceAccountJson().getBytes(StandardCharsets.UTF_8)); + googleCredentials = GoogleCredentials.fromStream(jsonInputStream).createScoped(scopes); } else if (Boolean.TRUE.equals(config.isServiceAccountFilePath()) && !Strings.isNullOrEmpty( config.getServiceAccountFilePath())) { googleCredentials = - GoogleCredentials.fromStream(new FileInputStream(config.getServiceAccountFilePath())); + GoogleCredentials.fromStream( + new FileInputStream(config.getServiceAccountFilePath())).createScoped(scopes); } else { googleCredentials = - GoogleCredentials.getApplicationDefault().createScoped(getRequiredScopes()); + GoogleCredentials.getApplicationDefault().createScoped(scopes); } return new HttpCredentialsAdapter(googleCredentials); }