客户端库和 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. 如果您使用的是本地 shell,请为您的用户 账号创建本地身份验证凭证:

    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 版客户端库相关的更多资源的链接: