Skip to content

Commit

Permalink
Add scopes when service account json/filePath is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
itsankit-google committed Feb 9, 2024
1 parent f7c2c32 commit c58f79f
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> 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);
}
Expand Down

0 comments on commit c58f79f

Please sign in to comment.