API 要求

設定專案並宣告 Java 專用 Google API 用戶端程式庫的依附元件後,請按照下列步驟提出要求。本頁面的程式碼片段使用 Cloud Resource Manager API 第 3 版。

事前準備

將您呼叫的 API 所產生的服務程式庫,以及 Java 專用 Google Auth 程式庫 (com.google.auth:google-auth-library-oauth2-http) 新增至專案。使用 com.google.cloud:libraries-bom 可對齊 google-api-client 和 google-auth-library-oauth2-http 的相容版本 (詳情請參閱設定操作說明)。

將下方的版本預留位置替換為 Maven Central 的最新版本:

Maven

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>LATEST_BOM_VERSION</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-cloudresourcemanager</artifactId>
    <version>LATEST_VERSION</version>
  </dependency>
  <dependency>
    <groupId>com.google.auth</groupId>
    <artifactId>google-auth-library-oauth2-http</artifactId>
  </dependency>
</dependencies>

Gradle

repositories {
  mavenCentral()
}

dependencies {
  implementation platform('com.google.cloud:libraries-bom:LATEST_BOM_VERSION')
  implementation 'com.google.apis:google-api-services-cloudresourcemanager:LATEST_VERSION'
  implementation 'com.google.auth:google-auth-library-oauth2-http'
}

步驟 1:驗證

將 com.google.auth.oauth2.GoogleCredentials 例項化。GoogleCredentials 和 com.google.auth.http.HttpCredentialsAdapter 都來自 com.google.auth:google-auth-library-oauth2-http 構件。雖然 google-api-client 包含 google-auth-library-oauth2-http 做為遞移依附元件,但請明確宣告並使用 com.google.cloud:libraries-bom (或 com.google.auth:google-auth-library-bom) 管理其版本,以便專案使用最新版本。

Google Cloud 使用者可以透過 GoogleCredentials.getApplicationDefault() 取得應用程式預設憑證。

GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();

如要進一步瞭解驗證,請參閱 Java 專用 Google Auth 程式庫指南中的「搭配 google-http-client 使用憑證」。

步驟 2:例項化服務類別

Google 服務有一或多個版本。服務類別代表服務的版本,是 AbstractGoogleJsonClient 的子類別。例如 com.google.api.services.cloudresourcemanager.v3.CloudResourceManager。

服務類別的建構工具類別會採用 3 個參數:

此外,請使用應用程式名稱呼叫建構工具的 setApplicationName() 方法。這會使用應用程式名稱設定 UserAgent 標頭,有助於透過記錄排解問題。

程式碼如下:

HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
CloudResourceManager.Builder resourceManagerBuilder =
    new CloudResourceManager.Builder(
        transport, jsonFactory, new HttpCredentialsAdapter(credentials))
        .setApplicationName("Example Java App");
CloudResourceManager cloudResourceManager = resourceManagerBuilder.build();

步驟 3:建立資源物件

資源類別代表服務管理的資源類型。這個類別定義為服務類別的內部類別。您可以使用服務類別中的方法存取這些方法。

例如,您可以取得 CloudResourceManager 類別的「Projects」資源:

import com.google.api.services.cloudresourcemanager.v3.CloudResourceManager.Projects;

...

Projects projects = cloudResourceManager.projects();

步驟 4:建立要求物件

針對資源物件執行的作業會以要求類別表示。可用的作業取決於資源類別。舉例來說,提供「create」、「get」和「delete」方法的資源類別,分別包含「Create」、「Get」和「Delete」要求類別。

以 Cloud Resource Manager 為例,您可以呼叫 projects.get 方法來建立 Get 要求物件:

Get get = projects.get("projects/your-project-id");

步驟 5:執行要求

要求物件具有執行要求的 execute() 方法。這項呼叫會對 Google 服務執行 HTTP 要求,並將 JSON 回應反序列化為模型類別。例如,Get 要求物件的 execute() 方法會傳回 Project 物件:

Project project = get.execute();
System.out.println("Project name: " + project.getDisplayName());

摘要

完成這些步驟後,您就可以使用適用於 Java 的 Google API 用戶端程式庫發出要求。以下程式碼片段結合了使用 Resource Manager 服務的所有步驟。

package com.example;

import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.cloudresourcemanager.v3.CloudResourceManager;
import com.google.api.services.cloudresourcemanager.v3.CloudResourceManager.Projects;
import com.google.api.services.cloudresourcemanager.v3.CloudResourceManager.Projects.Get;
import com.google.api.services.cloudresourcemanager.v3.model.Project;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;

public class ResourceManagerSample {
  public static void main(String[] arguments) throws Exception {
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();

    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
    CloudResourceManager.Builder resourceManagerBuilder =
        new CloudResourceManager.Builder(
            transport, jsonFactory, new HttpCredentialsAdapter(credentials))
            .setApplicationName("Example Java App");
    CloudResourceManager cloudResourceManager = resourceManagerBuilder.build();

    Projects projects = cloudResourceManager.projects();

    Get get = projects.get("projects/your-project-id");

    Project project = get.execute();
    System.out.println("Project display name: " + project.getDisplayName());
  }
}