You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Node API是否支持Deployment Manager调用?求Node.js客户端库及认证方案

GCP Deployment Manager Node.js客户端库与服务器到服务器OAuth2实现

Node.js客户端库

  • 你提到的google-api-nodejs-client库确实支持Deployment Manager API,虽然官方没有提供专门示例,但库已经封装了完整的v2版本接口。直接通过google.dm('v2')就能初始化客户端实例,调用部署的创建、查询、删除等所有REST API对应的方法。
  • 先安装依赖:npm install googleapis

服务器到服务器OAuth2认证(服务账号方式)

采用GCP服务账号实现无交互的服务器到服务器认证,步骤如下:

  1. 在GCP控制台创建服务账号,分配Deployment Manager相关权限(比如roles/deploymentmanager.editor),并下载JSON格式的密钥文件。
  2. 在代码中通过GoogleAuth类加载密钥,指定所需的权限范围(推荐使用https://www.googleapis.com/auth/cloud-platform,覆盖大部分GCP服务权限)。
  3. 将认证后的客户端绑定到Deployment Manager API实例,即可发起授权请求。

示例代码

const {google} = require('googleapis');

async function listDeployments() {
  // 初始化认证客户端
  const auth = new google.auth.GoogleAuth({
    keyFilename: './service-account-key.json', // 替换为你的密钥文件路径
    scopes: ['https://www.googleapis.com/auth/cloud-platform'],
  });
  const authClient = await auth.getClient();

  // 创建Deployment Manager客户端
  const deploymentManager = google.dm({
    version: 'v2',
    auth: authClient,
  });

  // 调用API列出项目下的部署
  const result = await deploymentManager.deployments.list({
    project: 'your-gcp-project-id', // 替换为你的项目ID
  });

  console.log('现有部署列表:', result.data.deployments);
}

// 执行方法
listDeployments().catch(err => console.error('请求失败:', err));

内容的提问来源于stack exchange,提问作者JayantSeth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 10:10:15