用戶端程式庫和 SDK

本頁說明如何開始使用 Google Security Operations API 適用的 Cloud 用戶端程式庫。有了用戶端程式庫,您可以透過支援的語言,更輕鬆地存取Google Cloud API。雖然您可以直接向伺服器發出原始要求來使用Google Cloud API,但用戶端程式庫提供簡化功能,可大幅減少需要編寫的程式碼數量。

如要進一步瞭解 Cloud 用戶端程式庫和舊版 Google API 用戶端程式庫,請參閱「用戶端程式庫說明」。

安裝用戶端程式庫

C++

請按照 Quickstart操作。

C#

從 NuGet 安裝 Google.Cloud.Chronicle.V1 套件。

詳情請參閱「設定 C# 開發環境」。

Go

go get cloud.google.com/go/chronicle/apiv1

詳情請參閱「設定 Go 開發環境」。

Java

如果您使用 Maven,請將下列指令新增到 pom.xml 檔案中。如要進一步瞭解 BOM,請參閱 Google Cloud Platform 程式庫 BOM

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

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-chronicle</artifactId>
  </dependency>
</dependencies>

如果您使用 Gradle,請將下列指令新增到依附元件中:

implementation 'com.google.cloud:google-cloud-chronicle:0.32.0'

如果您使用 sbt,請在依附元件中加入以下指令:

libraryDependencies += "com.google.cloud" % "google-cloud-chronicle" % "0.32.0"

詳情請參閱「設定 Java 開發環境」。

Node.js

npm install @google-cloud/chronicle

詳情請參閱「設定 Node.js 開發環境」。

PHP

composer require google/cloud-chronicle

詳情請參閱「在 Google Cloud 上使用 PHP」。

Python

pip install --upgrade google-cloud-chronicle

詳情請參閱「設定 Python 開發環境」。

Ruby

gem install google-cloud-chronicle-v1

詳情請參閱「設定 Ruby 開發環境」。

設定驗證方法

為驗證向 Google Cloud API 發出的呼叫,用戶端程式庫支援應用程式預設憑證 (ADC);程式庫會在定義的一組位置中尋找憑證,並使用這些憑證驗證向 API 發出的要求。有了 ADC,無需修改應用程式程式碼,就能在各種環境 (例如本機開發環境或正式環境),為應用程式提供憑證。

在正式環境中,設定 ADC 的方式取決於服務和背景。詳情請參閱「設定應用程式預設憑證」。

在本機開發環境中,您可以使用與 Google 帳戶相關聯的憑證設定 ADC:

  1. 安裝 Google Cloud CLI。 完成後,執行下列指令來初始化 Google Cloud CLI:

    gcloud init

    若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  2. 如果您使用本機殼層,請為使用者帳戶建立本機驗證憑證:

    gcloud auth application-default login

    如果您使用 Cloud Shell,則不需要執行這項操作。

    如果系統傳回驗證錯誤,且您使用外部識別資訊提供者 (IdP),請確認您已 使用聯合身分登入 gcloud CLI

    登入畫面會隨即顯示。登入後,您的憑證會儲存在 ADC 使用的本機憑證檔案中。

使用用戶端程式庫

以下範例說明如何使用用戶端程式庫列出參照清單。

C++

#include "google/cloud/chronicle/v1/chronicle_client.h"
#include "google/cloud/common_options.h"
#include <iostream>
#include <string>

int main(int argc, char* argv[]) {
  // TODO(developer): Replace these variables before running the sample.
  std::string const project_id = "";
  std::string const instance_id = "";
  std::string const location = "us"; 
  std::string const endpoint = "us-chronicle.googleapis.com";

  namespace chronicle = ::google::cloud::chronicle_v1;

  auto options = google::cloud::Options{}.set<google::cloud::EndpointOption>(endpoint);
  auto client = chronicle::ChronicleClient(chronicle::MakeChronicleConnection(options));

  // Construct the parent resource name
  // Format: projects/{project}/locations/{location}/instances/{instance}
  std::string parent = "projects/" + project_id + "/locations/" + location + "/instances/" + instance_id;

  google::cloud::chronicle::v1::ListReferenceListsRequest request;
  request.set_parent(parent);
  std::cout << "Listing reference lists for parent: " << parent << std::endl;
  for (auto const& reference_list : client.ListReferenceLists(request)) {
    if (!reference_list) {
      std::cerr << "Error listing reference lists: " << reference_list.status().message() << std::endl;
      return 1;
    }
    std::cout << "Name: " << reference_list->name() <<std::endl;
    std::cout << "Display Name: " << reference_list->display_name() << std::endl;
  }
  return 0;
}

C#

using Google.Cloud.Chronicle.V1;

// TODO(developer): Replace these variables before running the sample.
const string project  = "";
const string location = "us";
const string instance = "";
const string endpoint = "us-chronicle.googleapis.com";

ReferenceListServiceClient client = new ReferenceListServiceClientBuilder
{
  Endpoint = endpoint,
}.Build();

string parent = InstanceName.FromProjectLocationInstance(project, location, instance).ToString();

foreach (ReferenceList referenceList in client.ListReferenceLists(parent))
{
  Console.WriteLine($"Name:        {referenceList.Name}");
  Console.WriteLine($"Description: {referenceList.Description}");
}

Go

package main
import (
  "context"
  "fmt"
  "log"
  chronicle "cloud.google.com/go/chronicle/apiv1"
  chroniclepb "cloud.google.com/go/chronicle/apiv1/chroniclepb"
  "google.golang.org/api/iterator"
  "google.golang.org/api/option"
)

// TODO(developer): Replace these variables before running the sample.
const (
  project    = ""
  location   = "us"
  instance   = ""
  apiEndpoint = "us-chronicle.googleapis.com:443"
)

func main() {
  ctx := context.Background()
  client, err := chronicle.NewReferenceListClient(ctx, option.WithEndpoint(apiEndpoint),)
  if err != nil {
    log.Fatalf("failed to create client: %v", err)
  }
  defer client.Close()

  parent := fmt.Sprintf("projects/%s/locations/%s/instances/%s", project, location, instance)

  req := &chroniclepb.ListReferenceListsRequest{
    Parent: parent,
  }

  it := client.ListReferenceLists(ctx, req)
  for {
    resp, err := it.Next()
    if err == iterator.Done {
      break
    }
    if err != nil {
      log.Fatalf("error listing reference lists: %v", err)
    }
    fmt.Printf("Name: %s\n", resp.GetName())
    fmt.Printf("Description: %s\n", resp.GetDescription())
    fmt.Printf("---\n")
    }
}

Java

package com.example;

import com.google.cloud.chronicle.v1.InstanceName;
import com.google.cloud.chronicle.v1.ReferenceList;
import com.google.cloud.chronicle.v1.ReferenceListServiceClient;
import com.google.cloud.chronicle.v1.ReferenceListServiceSettings;

public class ListReferenceLists {
  // TODO(developer): Replace these variables before running the sample.
  private static final String PROJECT  = "";
  private static final String LOCATION = "us";
  private static final String INSTANCE = "";
  private static final String ENDPOINT = "us-chronicle.googleapis.com:443";

  public static void main(String[] args) throws Exception {
    ReferenceListServiceSettings settings = ReferenceListServiceSettings.newBuilder().setEndpoint(ENDPOINT).build();

    try (ReferenceListServiceClient client = ReferenceListServiceClient.create(settings)) {
      String parent = InstanceName.of(PROJECT, LOCATION, INSTANCE).toString();
      for (ReferenceList referenceList : client.listReferenceLists(parent).iterateAll()) {
        System.out.println("Name:        " + referenceList.getName());
        System.out.println("Description: " + referenceList.getDescription());
        System.out.println("---");
      }
    }
  }
}

Node.js

'use strict';

function main(parent) {
  const {ReferenceListServiceClient} = require('@google-cloud/chronicle').v1;

  const chronicleClient = new ReferenceListServiceClient({
    apiEndpoint: 'us-chronicle.googleapis.com',
  });

  async function callListReferenceLists() {
    // TODO(developer): Replace these variables before running the sample.
    const request = {
      parent: 'projects/<project-number>/locations/us/instances/<instance-id>'
    };

    const iterable = chronicleClient.listReferenceListsAsync(request);
    for await (const response of iterable) {
        console.log(response);
    }
  }

  callListReferenceLists();
}

process.on('unhandledRejection', err=> {
  console.error(err.message);
  process.exitCode = 1;
});
main(...process.argv.slice(2));

PHP

use Google\ApiCore\ApiException;
use Google\ApiCore\PagedListResponse;
use Google\Cloud\Chronicle\V1\Client\ReferenceListServiceClient;
use Google\Cloud\Chronicle\V1\ListReferenceListsRequest;
use Google\Cloud\Chronicle\V1\ReferenceList;

// TODO(developer): Replace these variables before running the sample.
const PROJECT  = '';
const LOCATION = 'us';
const INSTANCE = '';
const ENDPOINT = 'us-chronicle.googleapis.com';

$client = new ReferenceListServiceClient([
    'apiEndpoint' => ENDPOINT,
]);

$parent = ReferenceListServiceClient::instanceName(PROJECT, LOCATION, INSTANCE);

$request = (new ListReferenceListsRequest())->setParent($parent);

try {
    /** @var PagedListResponse $response /
    $response = $client->listReferenceLists($request);

    /* @var ReferenceList $referenceList */
    foreach ($response as $referenceList) {
        printf('Name:        %s' . PHP_EOL, $referenceList->getName());
        printf('Description: %s' . PHP_EOL, $referenceList->getDescription());
        printf('---' . PHP_EOL);
    }
} catch (ApiException $ex) {
    printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}

Python

from google.cloud import chronicle_v1

# TODO(developer): Replace these variables before running the sample.
PROJECT_ID = ""
LOCATION = "us"
INSTANCE_ID = ""

def list_reference_lists():
  client = chronicle_v1.ReferenceListServiceClient(client_options={"api_endpoint":"us-chronicle.googleapis.com"})
  parent = f"projects/{PROJECT_ID}/locations/{LOCATION}/instances/{INSTANCE_ID}"
  response = client.list_reference_lists(parent=parent)
  for reference_list in response:
    print(f"Name: {reference_list.name}")
    print(f"Display Name: {reference_list.display_name}")

if __name__=="__main__":
  list_reference_lists()

Ruby

require "google/cloud/chronicle/v1"

# TODO(developer): Replace these variables before running the sample.
PROJECT_ID  = ""
LOCATION    = "us"
INSTANCE_ID = ""
ENDPOINT    = "us-chronicle.googleapis.com"

client = Google::Cloud::Chronicle::V1::ReferenceListService::Client.new do |config|
  config.endpoint = ENDPOINT
end

parent = "projects/#{PROJECT_ID}/locations/#{LOCATION}/instances/#{INSTANCE_ID}"

request = Google::Cloud::Chronicle::V1::ListReferenceListsRequest.new(parent: parent)

begin
  response = client.list_reference_lists request

  response.each do |reference_list|
    puts "Name:        #{reference_list.name}"
    puts "Description: #{reference_list.description}"
  end
rescue Google::Cloud::Error => e
  puts "API call failed: #{e.message}"
end

其他資源

C++

下方列出與 C++ 用戶端程式庫相關的其他資源連結:

C#

下方列出與 C++ 用戶端程式庫相關的其他資源連結:

Go

下方列出與 Go 用戶端程式庫相關的其他資源連結:

Java

下方列出與 Java 用戶端程式庫相關的其他資源連結:

Node.js

下方列出與 Node.js 用戶端程式庫相關的其他資源連結:

PHP

下方列出與 PHP 用戶端程式庫相關的其他資源連結:

Python

下方列出與 Python 用戶端程式庫相關的其他資源連結:

Ruby

下方列出與 Ruby 用戶端程式庫相關的其他資源連結: