Skip to content

Commit

Permalink
Make getProjectId support GAE standard
Browse files Browse the repository at this point in the history
  • Loading branch information
shinfan committed Mar 9, 2017
1 parent a32c41a commit 60c20aa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
5 changes: 5 additions & 0 deletions google-cloud-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>1.9.50</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.appengine.api.appidentity.AppIdentityService;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
import com.google.appengine.api.utils.SystemProperty;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.spi.ServiceRpcFactory;
Expand Down Expand Up @@ -384,18 +387,11 @@ private static boolean isWindows() {

protected static String getAppEngineProjectId() {
try {
Class<?> factoryClass =
Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory");
Class<?> serviceClass =
Class.forName("com.google.appengine.api.appidentity.AppIdentityService");
Method method = factoryClass.getMethod("getAppIdentityService");
Object appIdentityService = method.invoke(null);
method = serviceClass.getMethod("getServiceAccountName");
String serviceAccountName = (String) method.invoke(appIdentityService);
AppIdentityService service = AppIdentityServiceFactory.getAppIdentityService();
String serviceAccountName = service.getServiceAccountName();
int indexOfAtSign = serviceAccountName.indexOf('@');
return serviceAccountName.substring(0, indexOfAtSign);
} catch (Exception ignore) {
// return null if can't determine
return null;
}
}
Expand Down

0 comments on commit 60c20aa

Please sign in to comment.