资讯详情 google-api-nodejs-client 中的 reseller 模块:Google Workspace Reseller API 的 Node.js 落地指南
📅 2026/10/15 5:46:51
后端【免费下载链接】google-api-nodejs-clientGoogles officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.项目地址https://gitcode.com/gh_mirrors/go/google-api-nodejs-client点击查看免费下载导读本文以 reseller 模块文档 为核心系统讲解如何在 google-api-nodejs-client 中安装、初始化并使用googleapis/reseller客户端规模化完成 Google Workspace原 G Suite分销场景下的下单、客户信息查看、订阅生命周期管理等操作。读完本文你将掌握 reseller API v1 的完整方法清单、核心数据模型、OAuth 2.0 权限范围以及从查询客户到下单、升级套餐、变更席位、暂停/恢复订阅的端到端实战代码。1. reseller 模块是什么根据 reseller 模块文档 的定义该模块封装的是Google Workspace Reseller APIv1其使命是Perform common functions that are available on the Channel Services console at scale, like placing orders and viewing customer information即在 Google 渠道服务Channel Services控制台之外以编程方式规模化地完成常见运营操作——典型场景包括下单订购 Google Workspace 订阅placing orders查看和管理客户信息viewing customer information管理订阅的套餐、席位seats、续订与暂停/恢复。从代码结构上看v1.ts 中Reseller类聚合了三大资源子类export class Reseller { context: APIRequestContext; customers: Resource$Customers; resellernotify: Resource$Resellernotify; subscriptions: Resource$Subscriptions; constructor(options: GlobalOptions, google?: GoogleConfigurable) { this.context {_options: options || {}, google}; this.customers new Resource$Customers(this.context); this.resellernotify new Resource$Resellernotify(this.context); this.subscriptions new Resource$Subscriptions(this.context); } }即所有操作都收敛在reseller.customers、reseller.resellernotify、reseller.subscriptions三个命名空间下分别对应客户账户管理、Pub/Sub 事件通知与订阅生命周期管理三大主题。2. 安装与依赖2.1 独立子包安装reseller 模块既可以在整体包googleapis中使用也可以作为独立的子包安装这是 reseller 模块文档 推荐的安装方式$ npm install googleapis/reseller根据 package.json该子包当前版本为13.0.1许可证为Apache-2.0运行时要求Node.js 22.0.0核心依赖为googleapis-common ^9.0.0提供createAPIRequest、AuthPlus、GoogleConfigurable等基础设施。2.2 在整体包中使用也可以直接安装googleapis并通过顶层命名空间访问const {google} require(googleapis); const reseller google.reseller(v1);无论哪种方式最终都会经由 index.ts 中导出的reseller()工厂函数完成实例化export const VERSIONS { v1: reseller_v1.Reseller, }; export function reseller(version: v1): reseller_v1.Reseller; export function reseller(options: reseller_v1.Options): reseller_v1.Reseller;从 index.ts 可以看出目前该模块仅支持 v1 一个版本调用形式为reseller(v1)或在需要覆盖默认选项时传入reseller({version: v1, auth: ...})。3. 认证与权限范围OAuth 2.0Reseller API 属于面向渠道分销商的受控 API必须使用OAuth 2.0授权支持 API Key 的只读场景有限官方建议使用服务账户 域内委派或 OAuth 客户端。v1.ts 中StandardParameters声明了通用的鉴权入口auth可传入OAuth2Client、JWT、Compute、UserRefreshClient、BaseExternalAccountClient或GoogleAuth亦可通过access_token、key、oauth_token等标准参数直接传递凭证。整个模块涉及两个 OAuth 2.0 范围见各方法示例代码Scope说明适用方法https://www.googleapis.com/auth/apps.order读写权限可下单、修改订阅全部写操作https://www.googleapis.com/auth/apps.order.readonly只读权限customers.get、subscriptions.get、subscriptions.list、resellernotify.getwatchdetails推荐的初始化方式是使用GoogleAuth并绑定默认客户端与 v1.ts 中各方法的示例一致const {google} require(googleapis); async function main() { const auth new google.auth.GoogleAuth({ scopes: [ https://www.googleapis.com/auth/apps.order, https://www.googleapis.com/auth/apps.order.readonly, ], }); // Acquire an auth client, and bind it to all future calls const authClient await auth.getClient(); google.options({auth: authClient}); const reseller google.reseller(v1); // 后续所有 reseller 调用自动携带该客户端 } main().catch(e { console.error(e); throw e; });注意若以子包googleapis/reseller独立使用认证方式一致只是通过googleapis-common的AuthPlus获取 auth 客户端index.ts 中同样导出了auth。4. customers客户账户管理customers资源用于管理分销客户账户一个客户由主域名和物理地址描述Schema$Customer。它提供四个方法方法HTTP端点说明customers.getGET/apps/reseller/v1/customers/{customerId}查询已有客户账户customers.insertPOST/apps/reseller/v1/customers创建新客户账户或转入已有客户的订阅customers.patchPATCH/apps/reseller/v1/customers/{customerId}局部更新客户设置customers.updatePUT/apps/reseller/v1/customers/{customerId}整体更新客户设置4.1 查询客户const res await reseller.customers.get({ // 可以是客户主域名或 Google 生成的唯一标识域名变更后旧域名不可用建议持久化存储 customerId customerId: example.com, }); console.log(res.data);4.2 创建新客户创建客户时customerDomain、postalAddress为必填项其中countryCode必须是经批准可转售 Google 产品的国家/地区代码ISO 3166。创建后还需为客户配置管理员由管理员登录 Admin 控制台签署G Suite via Reseller协议后账户才激活——经销商不得代客户签署该协议见 customers.insert 的 JSDoc。const res await reseller.customers.insert({ // 可选转移直客或他方经销商客户订阅时必填十六进制鉴权令牌 customerAuthToken: d41d8cd98f00b204e9800998ecf8427e, requestBody: { customerDomain: example.com, // 必填不要带 www 前缀 alternateEmail: adminexample.com, // 备用联系邮箱须与主域不同 customerType: domain, // domain默认或 team postalAddress: { contactName: Jane Doe, organizationName: Example, Inc., addressLine1: 1600 Amphitheatre Pkwy, locality: Mountain View, region: CA, postalCode: 94043, countryCode: US, }, }, }); console.log(res.data);4.3 更新客户patch支持 patch 语义的局部更新update整体覆盖式更新PUT。需要注意customerType无法通过 API 直接修改但team类型客户可自行完成域名验证后自动变为domain见 customers.patch 的 JSDoc。5. subscriptions订阅生命周期管理subscriptions是 reseller 模块的方法最丰富的资源定义了一个客户支付方案payment plan、SKU、用户许可证seats、30 天试用与续订选项之间的完整关系Schema$Subscriptions。共提供9 个方法方法HTTP端点相对 /apps/reseller/v1说明subscriptions.insertPOST/customers/{customerId}/subscriptions创建或转入订阅subscriptions.getGET/customers/{customerId}/subscriptions/{subscriptionId}查询单个订阅subscriptions.listGET/subscriptions列出经销商管理的全部/某客户的订阅subscriptions.activatePOST/customers/{cid}/subscriptions/{sid}/activate恢复被经销商暂停的订阅subscriptions.suspendPOST/customers/{cid}/subscriptions/{sid}/suspend暂停活跃订阅subscriptions.changePlanPOST/customers/{cid}/subscriptions/{sid}/changePlan变更套餐如试用/弹性转年度subscriptions.changeSeatsPOST/customers/{cid}/subscriptions/{sid}/changeSeats变更用户许可证数量subscriptions.changeRenewalSettingsPOST/customers/{cid}/subscriptions/{sid}/changeRenewalSettings修改年度套餐续订设置subscriptions.startPaidServicePOST/customers/{cid}/subscriptions/{sid}/startPaidService30 天试用立即转付费subscriptions.deleteDELETE/customers/{cid}/subscriptions/{subscriptionId}取消/降级/暂停由deletionType决定以上端点均来自 v1.ts 中各方法的rootUrl url拼接实现rootUrl统一为https://reseller.googleapis.com/见 customers.get 实现 等。5.1 创建订阅const res await reseller.subscriptions.insert({ customerId: example.com, // 可选已有同产品不同 SKU 订阅时的动作SWITCH 时必须传 sourceSkuId action: SWITCH, sourceSkuId: Google-Apps-For-Business, requestBody: { skuId: Google-Apps-For-Business, plan: {planName: ANNUAL_MONTHLY_PAY}, seats: {numberOfSeats: 10}, purchaseOrderId: PO-2026-001, // 可选最多 80 字符会显示在账单上 }, });5.2 列出订阅list支持按客户与名称前缀过滤并支持分页默认每页 20 条maxResults可调nextPageToken续页const res await reseller.subscriptions.list({ customerId: example.com, customerNamePrefix: exa, // 匹配 exam.com、example20.com、example.com maxResults: 50, pageToken: ..., }); console.log(res.data.subscriptions);5.3 变更套餐、席位与续订changePlan请求体使用 Schema$ChangePlanRequestplanName必填。可选值包括ANNUAL_MONTHLY_PAY年度承诺、按月付款注意API 响应中返回为ANNUALANNUAL_YEARLY_PAY年度承诺、按年付款FLEXIBLE弹性套餐TRIAL30 天免费试用。dealCode最多 100 字符可选用于享受折扣价seats为必填。changeSeats请求体为 Schema$SeatsnumberOfSeats仅适用于年度套餐且设定后到续订前不可减少计费按该值进行maximumNumberOfSeats仅适用于FLEXIBLE/TRIAL套餐。changeRenewalSettings请求体为 Schema$RenewalSettings仅适用于年度承诺套餐renewalType为必填。// 变更套餐 await reseller.subscriptions.changePlan({ customerId: example.com, subscriptionId: subscription-id-123, requestBody: { planName: ANNUAL_YEARLY_PAY, seats: {numberOfSeats: 25}, purchaseOrderId: PO-2026-002, }, }); // 变更席位 await reseller.subscriptions.changeSeats({ customerId: example.com, subscriptionId: subscription-id-123, requestBody: {numberOfSeats: 30}, }); // 修改续订设置 await reseller.subscriptions.changeRenewalSettings({ customerId: example.com, subscriptionId: subscription-id-123, requestBody: {renewalType: RENEW_WITH_COMMITMENT}, });5.4 暂停、恢复、取消与转付费suspend仅适用于ACTIVE状态的付费订阅。FLEXIBLE套餐暂停后停止计费年度套餐暂停不改变原承诺的续订日期、且不自动续订——若在原续订日之后才恢复会从恢复当天起生成一个新的年度订阅。官方建议暂停不要超过 60 天超期可能被取消见 subscriptions.suspend 的 JSDoc。activate仅能恢复由经销商主动暂停的订阅因滥用、待接受 ToS 等原因被 Google 暂停的订阅无法通过此接口恢复。startPaidService将 30 天试用订阅立即转为付费服务前提是已为该试用订阅设置好支付方案。deletedeletionType为必填查询参数决定是取消cancel、降级downgrade还是转移为直客transfer_to_direct。await reseller.subscriptions.suspend({ customerId: example.com, subscriptionId: subscription-id-123, }); await reseller.subscriptions.activate({ customerId: example.com, subscriptionId: subscription-id-123, }); await reseller.subscriptions.startPaidService({ customerId: example.com, subscriptionId: subscription-id-123, }); await reseller.subscriptions.delete({ customerId: example.com, subscriptionId: subscription-id-123, deletionType: cancel, // cancel | downgrade | transfer_to_direct });6. resellernotify基于 Pub/Sub 的事件通知resellernotify资源负责让经销商订阅 Google Cloud Pub/Sub 事件通知当订阅/客户发生变更时可实时感知。它提供 3 个方法Resource$Resellernotify方法HTTP说明resellernotify.registerPOST注册经销商开始接收通知需提供服务账号邮箱该账号将拥有创建的 Pub/Sub topicresellernotify.unregisterPOST注销经销商的通知接收resellernotify.getwatchdetailsGET查询当前 watch 详情// 注册serviceAccountEmailAddress 为将拥有 Pub/Sub topic 的服务账号 const res await reseller.resellernotify.register({ serviceAccountEmailAddress: reseller-notifymy-project.iam.gserviceaccount.com, }); console.log(res.data); // { topicName: projects/my-project/topics/... } // 查询 const details await reseller.resellernotify.getwatchdetails({}); console.log(details.data); // { // serviceAccountEmailAddresses: [...], // topicName: ... // } // 注销 await reseller.resellernotify.unregister({ serviceAccountEmailAddress: reseller-notifymy-project.iam.gserviceaccount.com, });对应响应模型为 Schema$ResellernotifyGetwatchdetailsResponse返回serviceAccountEmailAddresses数组与topicName和 Schema$ResellernotifyResource仅返回topicName。7. 核心数据模型速查以下 Schema 全部定义在 v1.ts 中也是类型声明TypeScript直接导出的Schema位置关键字段Schema$Addressv1.ts#L134addressLine1-3、contactName、countryCode、locality、organizationName、postalCode、region各字段上限 255 字符Schema$Customerv1.ts#L204customerDomain、customerDomainVerified、customerId、customerTypedomain/team、alternateEmail、phoneNumber须以开头加国家码、postalAddress、primaryAdmin、resourceUiUrl只读Schema$PrimaryAdminv1.ts#L249primaryEmailteam 客户的首席管理员创建时接收验证邮件Schema$Seatsv1.ts#L293licensedNumberOfSeats只读、maximumNumberOfSeatsFLEXIBLE/TRIAL 用、numberOfSeats年度套餐用续订前不可减Schema$ChangePlanRequestv1.ts#L179dealCode、planName必填、purchaseOrderId、seats必填Schema$RenewalSettingsv1.ts#L258renewalType续订时必填Schema$Subscriptionv1.ts#L314skuId、skuName、plan、seats、renewalSettings、trialSettings、dealCode、status、suspensionReasons、transferInfo、creationTimeEpoch 毫秒等Schema$Subscriptionsv1.ts#L399subscriptions列表 nextPageToken分页关于订阅statussuspensionReasons只读字段枚举了常见暂停原因v1.ts#L380PENDING_TOS_ACCEPTANCE客户未登录并接受 G Suite 转售服务条款RENEWAL_WITH_TYPE_CANCEL承诺期结束、服务被取消RESELLER_INITIATED经销商手动暂停TRIAL_ENDED试用到期未选择套餐OTHERGoogle 内部原因如滥用。只有全部暂停原因解除后订阅状态才会从SUSPENDED恢复为ACTIVE。8. 底层实现与调用链8.1 请求是如何发出的每个方法如 customers.get 的实现最终都会走统一模板const rootUrl options.rootUrl || https://reseller.googleapis.com/; const parameters { options: Object.assign( { url: (rootUrl /apps/reseller/v1/customers/{customerId}).replace(/([^:]\/)\//g, $1), method: GET, apiVersion: , }, options ), params, requiredParams: [customerId], pathParams: [customerId], context: this.context, }; return createAPIRequestSchema$Customer(parameters);createAPIRequest来自googleapis-common负责把params中的pathParams填充到 URL、合并查询参数与请求体并挂载 auth 客户端。开发者也可以通过options覆盖rootUrl如代理环境、method或encoding等底层行为。8.2 三种调用风格所有方法都重载了三种调用形式以 subscriptions.get 为例Promiseasync/awaitawait reseller.subscriptions.get({...})返回GaxiosResponseWithHTTP2Schema$Subscription响应数据在.data回调reseller.subscriptions.get({...}, callback)流式传入StreamMethodOptions时返回Readable流便于处理大响应或管道输出。8.3 模块注册与版本reseller 模块文档 中未展开的模块装配细节可从 index.ts 与 CHANGELOG.md 中读到该子包由生成器generator自动生成随 Discovery Document 更新而同步发布13.0.1版本2026-09 发布修复了 API 更新问题且近期版本将最低 Node 版本提升至 22。9. 实战一个完整的自动化运维脚本将以上能力组合即可写出一个典型的经销商自动化脚本——查询客户、检查订阅状态、为试用到期的订阅转付费并扩容const {google} require(googleapis); async function main() { const auth new google.auth.GoogleAuth({ scopes: [https://www.googleapis.com/auth/apps.order], }); const authClient await auth.getClient(); google.options({auth: authClient}); const reseller google.reseller(v1); // 1. 列出所有订阅分页遍历 let pageToken; do { const res await reseller.subscriptions.list({pageToken, maxResults: 100}); for (const sub of res.data.subscriptions || []) { console.log(sub.customerId, sub.skuName, sub.status); // 2. 试用中且临近到期 → 立即转付费 if (sub.trialSettings sub.trialSettings.isInTrial) { await reseller.subscriptions.startPaidService({ customerId: sub.customerId, subscriptionId: sub.subscriptionId, }); console.log(trial started to paid: ${sub.subscriptionId}); } // 3. 扩容年度套餐增加席位 await reseller.subscriptions.changeSeats({ customerId: sub.customerId, subscriptionId: sub.subscriptionId, requestBody: {numberOfSeats: 50}, }); } pageToken res.data.nextPageToken; } while (pageToken); } main().catch(e { console.error(e); throw e; });10. 许可与社区约定reseller 模块文档 明确该库基于Apache 2.0开源完整许可文本见仓库根目录 LICENSE。仓库同时提供了 CODE_OF_CONDUCT.md 与 CONTRIBUTING.md 供参与贡献时参考若在集成中发现缺陷可到仓库 issue 区反馈或在 Stack Overflow 上使用google-api-nodejs-client标签提问详见 README.md。总结模块定位googleapis/reseller是 Google Workspace Reseller API v1 的官方 Node.js 客户端聚焦客户、订阅、通知三大领域能力边界支持客户增删查改、订阅的全生命周期管理下单/查询/变更套餐/变更席位/续订/暂停/恢复/转付费/取消、基于 Pub/Sub 的通知注册与查询约束要点写操作需apps.order范围numberOfSeats年度套餐续订前不可减activate仅能恢复经销商主动暂停的订阅customerType不可通过 API 修改运行时要求 Node.js 22进一步阅读完整的类型定义与逐方法示例见 v1.ts模块装配逻辑见 index.ts版本演进见 CHANGELOG.md。赞分享后端【免费下载链接】google-api-nodejs-clientGoogles officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.项目地址https://gitcode.com/gh_mirrors/go/google-api-nodejs-client点击查看免费下载相关推荐google-api-nodejs-client 的 reseller 客户端包版本演进、Node 兼容性与 Google Workspace 渠道服务 API 实战google api nodejs client 的 reseller 客户端包版本演进、Node 兼容性与 Google Workspace 渠道服务 AP后端google-api-nodejs-client 中 orgpolicy 模块Google Cloud 组织策略 API 的 Node.js 接入指南google api nodejs client 中 orgpolicy 模块Google Cloud 组织策略 API 的 Node.js 接入指南 本指南后端google-api-nodejs-client 中的 certificatemanager 模块使用 Node.js 管理 Google Certificate Manager 证书google api nodejs client 中的 certificatemanager 模块使用 Node.js 管理 Google Certifica后端上一篇Flutter Thrio 教程下一篇5分钟搞定XGBoost机器学习新手的终极入门指南创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考