diff --git a/pom.xml b/pom.xml index 571aa50301a..0d5f3ea374c 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,8 @@ UTF-8 1.19.0 1.19.0 + 1.7 + 1.7 diff --git a/storage/json-api/src/main/java/StorageSample.java b/storage/json-api/src/main/java/StorageSample.java index 3f5442ae53c..49edae5e2cc 100644 --- a/storage/json-api/src/main/java/StorageSample.java +++ b/storage/json-api/src/main/java/StorageSample.java @@ -20,6 +20,7 @@ import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.storage.Storage; +import com.google.api.services.storage.StorageScopes; import com.google.api.services.storage.model.Bucket; import com.google.api.services.storage.model.ObjectAccessControl; import com.google.api.services.storage.model.Objects; @@ -61,6 +62,12 @@ public class StorageSample { private static Storage getService() throws IOException, GeneralSecurityException { if (null == storageService) { GoogleCredential credential = GoogleCredential.getApplicationDefault(); + // Depending on the environment that provides the default credentials (e.g. Compute Engine, + // App Engine), the credentials may require us to specify the scopes we need explicitly. + // Check for this case, and inject the Bigquery scope if required. + if (credential.createScopedRequired()) { + credential = credential.createScoped(StorageScopes.all()); + } HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); storageService = new Storage.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME).build();