Agent Platform Workbench 客户端库

本页面介绍了如何开始使用 Notebooks API 的 Cloud 客户端库。通过客户端库,您可以更轻松地使用支持的语言访问 Google Cloud API。虽然您可以通过向服务器发出原始请求来直接使用Google Cloud API,但客户端库可实现简化,从而显著减少您需要编写的代码量。

请参阅客户端库说明,详细了解 Cloud 客户端库和旧版 Google API 客户端库。

安装客户端库

C++

如需详细了解此客户端库的要求和安装依赖项,请参阅 GitHub README

C#

Install-Package Google.Cloud.Notebooks.V1 -Pre

如需了解详情,请参阅设置 C# 开发环境

Go

go get cloud.google.com/go/notebooks

如需了解详情,请参阅设置 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-notebooks</artifactId>
  </dependency>
</dependencies>

如果您使用的是 Gradle, 请将以下代码添加到您的依赖项中:

implementation 'com.google.cloud:google-cloud-notebooks:1.90.0'

如果您使用的是 sbt,请将 以下代码添加到您的依赖项中:

libraryDependencies += "com.google.cloud" % "google-cloud-notebooks" % "1.90.0"

如需了解详情,请参阅设置 Java 开发环境

Node.js

npm install @google-cloud/notebooks

如需了解详情,请参阅设置 Node.js 开发环境

PHP

composer require google/cloud

如需了解详情,请参阅在 Google Cloud 上使用 PHP

Python

Mac/Linux

pip install virtualenv
virtualenv ENVIRONMENT_NAME
source ENVIRONMENT_NAME/bin/activate
ENVIRONMENT_NAME/bin/pip install google-cloud-notebooks

Windows

pip install --upgrade google-cloud-notebooks
pip install virtualenv
virtualenv ENVIRONMENT_NAME
ENVIRONMENT_NAME\Scripts\activate
ENVIRONMENT_NAME\Scripts\pip.exe install google-cloud-notebooks

如需了解详情,请参阅设置 Python 开发环境

Ruby

gem install google-cloud-notebooks

如需了解详情,请参阅设置 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++

// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! [all]
#include "google/cloud/notebooks/v2/notebook_client.h"
#include "google/cloud/location.h"
#include <iostream>

int main(int argc, char* argv[]) try {
  if (argc != 3) {
    std::cerr << "Usage: " << argv[0] << " project-id location-id\n";
    return 1;
  }

  auto const location = google::cloud::Location(argv[1], argv[2]);

  namespace notebooks = ::google::cloud::notebooks_v2;
  auto client = notebooks::NotebookServiceClient(
      notebooks::MakeNotebookServiceConnection());

  for (auto i : client.ListInstances(location.FullName())) {
    if (!i) throw std::move(i).status();
    std::cout << i->DebugString() << "\n";
  }

  return 0;
} catch (google::cloud::Status const& status) {
  std::cerr << "google::cloud::Status thrown: " << status << "\n";
  return 1;
}
//! [all]

Node.js

// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

async function main(projectId, location) {
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'my-project';
  // const location = 'global';

  // Imports the Google Cloud Some API library
  const {NotebookServiceClient} = require('@google-cloud/notebooks');
  const client = new NotebookServiceClient();
  async function listInstances() {
    const [instances] = await client.listInstances({
      parent: `projects/${projectId}/locations/${location}`,
    });
    for (const instance of instances) {
      console.info(`instance: ${instance.name}`);
    }
  }
  listInstances();
}

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

其他资源

C++

以下列表包含与 C++ 版客户端库相关的更多资源的链接:

C#

以下列表包含与 C# 版客户端库相关的更多资源的链接:

Go

以下列表包含与 Go 版客户端库相关的更多资源的链接:

Java

以下列表包含与 Java 版客户端库相关的更多资源的链接:

Node.js

以下列表包含与 Node.js 版客户端库相关的更多资源的链接:

PHP

以下列表包含与 PHP 版客户端库相关的更多资源的链接:

Python

以下列表包含与 Python 版客户端库相关的更多资源的链接:

Ruby

以下列表包含与 Ruby 版客户端库相关的更多资源的链接: