diff --git a/storage/json-api/src/main/java/StorageFactory.java b/storage/json-api/src/main/java/StorageFactory.java index df47b13475e..ea91b414b90 100644 --- a/storage/json-api/src/main/java/StorageFactory.java +++ b/storage/json-api/src/main/java/StorageFactory.java @@ -29,8 +29,8 @@ /** * This class manages the details of creating a Storage service, including auth. */ +// [START authentication_application_default_credentials] public class StorageFactory { - private static Storage instance = null; public static synchronized Storage getService() throws IOException, GeneralSecurityException { @@ -45,9 +45,13 @@ private static Storage buildService() throws IOException, GeneralSecurityExcepti JsonFactory jsonFactory = new JacksonFactory(); GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); + // Depending on the environment that provides the default credentials (for + // example: Compute Engine, App Engine), the credentials may require us to + // specify the scopes we need explicitly. Check for this case, and inject + // the Cloud Storage scope if required. if (credential.createScopedRequired()) { - Collection bigqueryScopes = StorageScopes.all(); - credential = credential.createScoped(bigqueryScopes); + Collection scopes = StorageScopes.all(); + credential = credential.createScoped(scopes); } return new Storage.Builder(transport, jsonFactory, credential) @@ -55,3 +59,4 @@ private static Storage buildService() throws IOException, GeneralSecurityExcepti .build(); } } +// [END authentication_application_default_credentials]