diff --git a/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java b/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java index 741a4b27d..c0b3cab98 100644 --- a/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java @@ -564,7 +564,7 @@ public static ServiceAccountCredentials fromStream( /** Returns whether the scopes are empty, meaning createScoped must be called before use. */ @Override public boolean createScopedRequired() { - return scopes.isEmpty(); + return scopes.isEmpty() && defaultScopes.isEmpty(); } /** diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java index 604f68c60..e75a70257 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java @@ -488,7 +488,7 @@ public void createScopedRequired_emptyScopes() throws IOException { } @Test - public void createScopedRequired_nonEmptyScopes_false() throws IOException { + public void createScopedRequired_nonEmptyScopes() throws IOException { GoogleCredentials credentials = ServiceAccountCredentials.fromPkcs8( CLIENT_ID, CLIENT_EMAIL, PRIVATE_KEY_PKCS8, PRIVATE_KEY_ID, SCOPES); @@ -496,6 +496,15 @@ public void createScopedRequired_nonEmptyScopes_false() throws IOException { assertFalse(credentials.createScopedRequired()); } + @Test + public void createScopedRequired_nonEmptyDefaultScopes() throws IOException { + GoogleCredentials credentials = + ServiceAccountCredentials.fromPkcs8( + CLIENT_ID, CLIENT_EMAIL, PRIVATE_KEY_PKCS8, PRIVATE_KEY_ID, null, SCOPES); + + assertFalse(credentials.createScopedRequired()); + } + @Test public void fromJSON_getProjectId() throws IOException { MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();