我正在开发一个使用部署在AppEngine上的剩余端点启动Google VM实例的应用。
初始化Compute Engine API时,我会提供应用程序默认凭据(因为我希望这会返回AppEngine服务帐户凭据)
GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
List<String> scopes = new ArrayList<>();
// Set Google Compute Engine scope to Read-write.
scopes.add(ComputeScopes.COMPUTE);
credential = credential.createScoped(scopes);
}
return new Compute.Builder(httpTransport(), jsonFactory(), credential)
.setApplicationName(env.getProperty("spring.application.name"))
.build();
但是我最终遇到了错误:
java.io.IOException: Application Default Credentials failed to create the Google App Engine service account credentials
我正在使用库:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>${google-api-client.version}</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-compute</artifactId>
<version>${google-api-services-compute.version}</version>
</dependency>
答案 0 :(得分:2)
我通过使用 google-api-cient-appengine 而不是 google-api-client 解决了此问题:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-appengine</artifactId>
<version>${google-api-client-appengine.version}</version>
</dependency>
并使用
初始化 GoogleCredentialGoogleCredential credential = AppEngineCredentialWrapper.getApplicationDefault();