From a5205cbd8069aa5e4407932b915279fb287864ff Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Thu, 12 May 2016 15:01:21 -0700 Subject: [PATCH] Add missing Application Default Credentials region tags. See: https://github.com/GoogleCloudPlatform/java-docs-samples/commit/dceeb5f29b588ad26c0083af18a56f36b032c2b2#commitcomment-17458371 --- storage/json-api/src/main/java/StorageFactory.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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]