Как создать клиент Compute с использованием SDK Google API Services в Java - PullRequest
0 голосов
/ 29 апреля 2020

Я пытаюсь построить клиент Compute на основе файла ключа. JSON. Я смотрю на примеры, найденные здесь , но они устарели и больше не работают.

Я не могу найти ни одного примера в текущих официальных документах здесь .

Вот что я сейчас пытаюсь:

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.model.Instance;
import com.google.api.services.compute.model.InstanceList;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;

public class Application {
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    public static void main(String[] args) throws IOException, GeneralSecurityException {
        InputStream credentialsJSON = Application.class.getClassLoader().getResourceAsStream("mykey.json");
        JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        GoogleCredential cred = GoogleCredential.fromStream(credentialsJSON ,httpTransport,JSON_FACTORY);

        // Create Compute Engine object for listing instances.
        Compute compute = new Compute.Builder(httpTransport, JSON_FACTORY, cred.getRequestInitializer())
                .setApplicationName("myapplication")
                .build();

        InstanceList instanceList = compute.instances().list("PROJECT_NAME", "europe-west3-a").execute();
        for (Instance instance : instanceList.getItems()) {
            System.out.println(instance.getId());
        }
    }
}

Но выдает следующее сообщение об ошибке:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
  "code" : 401,
  "errors" : [ {
    "domain" : "global",
    "location" : "Authorization",
    "locationType" : "header",
    "message" : "Login Required.",
    "reason" : "required"
  } ],
  "message" : "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
  "status" : "UNAUTHENTICATED"
}

Я не понимаю, потому что файл получен проанализированным правильно. Кроме того, модель GoogleCredential , которую я использую, кажется устаревшей.

1 Ответ

0 голосов
/ 30 апреля 2020

Вам нужны эти две зависимости:

<dependencies>
    <dependency>
        <groupId>com.google.apis</groupId>
        <artifactId>google-api-services-compute</artifactId>
        <version>v1-rev20200311-1.30.9</version>
    </dependency>

    <dependency>
        <groupId>com.google.auth</groupId>
        <artifactId>google-auth-library-oauth2-http</artifactId>
        <version>0.20.0</version>
    </dependency>
</dependencies>

Хранилище зависимостей google-auth-library-oauth2-http можно найти здесь . Переход на этот новый метод работал для меня.

А вот рабочий код:

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.ComputeScopes;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;

public class GCPComputeClientHelper {
    private static Compute compute = null;

    protected GCPComputeClientHelper() {
        // Exists only to defeat instantiation
    }

    public static Compute getComputeInstance() throws GeneralSecurityException, IOException {
        if (compute == null) {
            compute = build();
        }
        return compute;
    }

    private static Compute build() throws GeneralSecurityException, IOException {
        // Create http transporter needed for Compute client
        HttpTransport HTTP_TRANSPORTER = GoogleNetHttpTransport.newTrustedTransport();

        // Read GCP service account credentials JSON key file
        InputStream serviceAccountJsonKey = GCPComputeClientHelper.class.getClassLoader().getResourceAsStream("mykeyfile.json");

        // Authenticate based on the JSON key file
        GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccountJsonKey);
        credentials = credentials.createScoped(ComputeScopes.CLOUD_PLATFORM);
        HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);

        // Create and return GCP Compute client
        return new Compute.Builder(HTTP_TRANSPORTER, JacksonFactory.getDefaultInstance(), requestInitializer)
                .setApplicationName("myapplication")
                .build();
    }

}
...