我正在尝试使用适用于Java的API客户端库创建一些GCP VM,但是我看到的示例似乎已经过时了。我从这个 https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/compute/cmdline/src/main/java/ComputeEngineSample.java 开始,但看起来GoogleCredential
类已被弃用。 经过一些研究,我想出了这个代码
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
if (credentials.createScopedRequired()) {
List<String> scopes = new ArrayList<>();
scopes.add(ComputeScopes.DEVSTORAGE_FULL_CONTROL);
scopes.add(ComputeScopes.DEVSTORAGE_READ_WRITE);
credentials = credentials.createScoped(scopes);
}
Compute compute = new Compute.Builder(httpTransport, jsonFactory, null)
.setApplicationName("et").build();
Compute.Instances.List instancesList = compute.instances().list("<GCP-PROJECT>", "us-central1-a");
InstanceList executedOperation = instancesList.execute();
接下来,我将GOOGLE_APPLICATION_CREDENTIALS
环境变量设置为我的服务帐户 json 文件的路径并运行我的代码,但它失败并出现401 Unauthorized
错误。部分消息是这样的
"message" : "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential.
我看到的示例适用于其他Google产品,如存储,Pubsub等,但是我找不到有关如何使用凭据在Compute
服务上调用某些操作的任何相关示例。任何帮助将不胜感激。
我拿了谷歌样本,它对我有用。
我将其缩写为仅枚举实例。
PROJECT=
ZONE=
INSTANCE=
ACCOUNT=
gcloud projects create ${PROJECT}
gcloud alpha billing projects link ${PROJECT}
--billing-account=${BILLING}
gcloud services enable compute.googleapis.com
--project=${PROJECT}
# Create an instance to enumerate
gcloud compute instances create instance-1
--project=${PROJECT}
--zone=${ZONE}
--machine-type=f1-micro
--image-family=debian-10
--image-project=debian-cloud
--preemptible
gcloud iam service-accounts create ${ACCOUNT}
--project=${PROJECT}
gcloud iam service-accounts keys create ./${ACCOUNT}.json
--iam-account=${ACCOUNT}@${PROJECT}.iam.gserviceaccount.com
--project=${PROJECT}
# Overly broad permissions but...
gcloud projects add-iam-policy-binding ${PROJECT}
--member=serviceAccount:${ACCOUNT}@${PROJECT}.iam.gserviceaccount.com
--role=roles/compute.admin
export GOOGLE_APPLICATION_CREDENTIALS=./${ACCOUNT}.json
mvn exec:java -Dexec.mainClass="com.dazwilkin.gce.App"
产量(为清楚起见进行了编辑(:
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.dazwilkin.gce:gce >------------------------
[INFO] Building gce 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ gce ---
Hello Freddie!
================== Listing Compute Engine Instances ==================
{
"id" : "6020825766320745087",
"kind" : "compute#instance",
"name" : "instance-1",
}
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.088 s
[INFO] Finished at: 2020-05-13T17:54:51-07:00
[INFO] ------------------------------------------------------------------------
和pom.xml
:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dazwilkin.gce</groupId>
<artifactId>gce</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>gce</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.30.9</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-compute</artifactId>
<version>v1-rev235-1.25.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
更新:google-auth-library
从google-auth-library
文档中:
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.api.client.http.HttpRequestInitializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class App
{
public static void main(String[] args )
{
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
...
HttpRequestInitializer requestInitializer =
new HttpCredentialsAdapter(credentials);
Compute compute =
new Compute.Builder(httpTransport, JSON_FACTORY, requestInitializer)
.setApplicationName(APPLICATION_NAME)
.build();
...
}
与pom.xml
:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dazwilkin.gce</groupId>
<artifactId>gce</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>gce</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.30.9</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-compute</artifactId>
<version>v1-rev235-1.25.0</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-credentials</artifactId>
<version>0.20.0</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.20.0</version>
</dependency>
</dependencies>
</project>