Skip to content

Commit 3be6174

Browse files
committed
Extract projectId from GOOGLE_APPLICATION_CREDENTIALS if set
1 parent 545f099 commit 3be6174

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ Most `gcloud-java` libraries require a project ID. There are multiple ways to s
8383

8484
1. Project ID supplied when building the service options
8585
2. Project ID specified by the environment variable `GCLOUD_PROJECT`
86-
3. App Engine project ID
87-
4. Google Cloud SDK project ID
88-
5. Compute Engine project ID
86+
3. Project ID specified in the JSON credentials file pointed by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable
87+
4. App Engine project ID
88+
5. Google Cloud SDK project ID
89+
6. Compute Engine project ID
8990

9091
Authentication
9192
--------------

gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@
3030
import com.google.common.io.Files;
3131
import com.google.gcloud.spi.ServiceRpcFactory;
3232

33+
import org.json.JSONException;
34+
import org.json.JSONObject;
35+
import org.json.JSONTokener;
36+
3337
import java.io.BufferedReader;
3438
import java.io.File;
39+
import java.io.FileInputStream;
3540
import java.io.FileNotFoundException;
3641
import java.io.FileReader;
3742
import java.io.IOException;
@@ -378,7 +383,10 @@ protected String defaultHost() {
378383
protected String defaultProject() {
379384
String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME));
380385
if (projectId == null) {
381-
projectId = getAppEngineProjectId();
386+
projectId = serviceAccountProjectId();
387+
}
388+
if (projectId == null) {
389+
projectId = appEngineProjectId();
382390
}
383391
return projectId != null ? projectId : googleCloudProjectId();
384392
}
@@ -461,7 +469,7 @@ private static boolean isWindows() {
461469
return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");
462470
}
463471

464-
protected static String getAppEngineProjectId() {
472+
protected static String appEngineProjectId() {
465473
try {
466474
Class<?> factoryClass =
467475
Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory");
@@ -479,6 +487,20 @@ protected static String getAppEngineProjectId() {
479487
}
480488
}
481489

490+
protected static String serviceAccountProjectId() {
491+
String project = null;
492+
String credentialsPath = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
493+
if(credentialsPath != null) {
494+
try (InputStream credentialsStream = new FileInputStream(credentialsPath)) {
495+
JSONObject json = new JSONObject(new JSONTokener(credentialsStream));
496+
project = json.getString("project_id");
497+
} catch (IOException | JSONException ex) {
498+
// ignore
499+
}
500+
}
501+
return project;
502+
}
503+
482504
@SuppressWarnings("unchecked")
483505
public ServiceT service() {
484506
if (service == null) {

0 commit comments

Comments
 (0)