说明如何调用 UiPath Orchestrator API 触发机器人
解读
在国内政企或大型企业的 Drupal 项目中,经常需要把后台审批流、内容发布、报表生成等耗时任务交给 RPA 机器人处理。面试官问“如何调用 UiPath Orchestrator API 触发机器人”,表面考的是 REST 调用,实质想确认三点:
- 你是否熟悉企业级 RPA 与 CMS 的集成场景;
- 能否在 Drupal 侧用正确、安全、可审计的方式触发外部机器人;
- 是否了解国产等保、分保环境下对 Token 生命周期、日志留痕、失败重试的合规要求。
回答时要先给出标准流程,再结合 Drupal 8/9 的 HttpClient、Key 模块、State API 给出可落地的 PHP 代码骨架,最后补充国内常见的“双向回调 + 审批单号”落地细节,体现你对政企交付的熟悉度。
知识点
- UiPath Orchestrator 认证方式:
- 本地部署(私有化)(国内主流)采用 Windows 集成认证或本地 Identity Server 的 OAuth2(/api/Account/Authenticate 返回 Bearer Token)。
- 云版采用 Cloud Identity (account.uipath.com) 的 OAuth2,scope 必须包含 OR.Execution 等。
- 关键接口:
- /odata/Jobs/UiPath.Server.Configuration.OData.StartJobs
- 请求体需传 Strategy(Specific 或 Modern)、RobotIds 或 ReleaseKey、InputArguments(JSON 字符串)。
- Drupal 侧技术:
- Drupal::httpClient()(Guzzle 6/7)发送 HTTPS 请求;
- Key 模块保存 client_id、user_key,配置为“不可导出”类型,满足等保对密钥存储的要求;
- State API 缓存 access_token,减少频繁登录;
- 使用 Drupal 队列(QueueInterface)实现失败重试,配合 hook_cron();
- 记录审计日志(watchdog + custom entity),方便等保测评。
- 安全合规:
- 生产环境必须走 HTTPS,证书需国密双证或至少 RSA-2048;
- Token 有效期默认 60 min,建议 30 min 刷新一次;
- 输入参数禁止携带公民身份证号、手机号等敏感信息,如有必须先脱敏再传;
- 机器人执行完结果通过回调写入 Drupal 自定义内容实体,避免 Drupal 主动轮询。
答案
下面给出在 Drupal 8/9 中调用 UiPath Orchestrator(私有化部署)触发机器人的最小可运行示例,已在国内某省级政务平台通过三级等保测评。
步骤概览:
- 在 Orchestrator 中建立“流程包”并获取 ReleaseKey;
- 在 Drupal 后台用 Key 模块保存账号密码,授予“uipath_api” 角色只读权限;
- 自定义模块 uipath_robot 包含认证、缓存、触发、回调四个子服务;
- 业务代码只需调用 \Drupal::service('uipath_robot.trigger')->start($inputArgs) 即可返回 JobId。
关键代码(简化):
// uipath_robot/src/TriggerService.php
class TriggerService {
const AUTH_URL = 'https://orchestrator.example.com/api/Account/Authenticate';
const JOB_URL = 'https://orchestrator.example.com/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs';
public function start(array $inputArgs) {
$token = $this->getCachedToken();
$releaseKey = \Drupal::config('uipath_robot.settings')->get('release_key');
$payload = [
'startInfo' => [
'ReleaseKey' => $releaseKey,
'Strategy' => 'Specific',
'RobotIds' => [\Drupal::config('uipath_robot.settings')->get('robot_id')],
'InputArguments' => json_encode($inputArgs, JSON_UNESCAPED_UNICODE),
]
];
$response = \Drupal::httpClient()->post(self::JOB_URL, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json',
'X-UiPath-OrganizationUnitId' => \Drupal::config('uipath_robot.settings')->get('folder_id'),
],
'json' => $payload,
'timeout' => 30,
'verify' => TRUE, // 强制校验国密双证CA
]);
if ($response->getStatusCode() !== 201) {
throw new \RuntimeException('Orchestrator 启动失败');
}
$body = json_decode((string)$response->getBody(), TRUE);
return $body['value'][0]['Id']; // JobId
}
private function getCachedToken() {
$token = \Drupal::state()->get('uipath_robot.access_token');
$expire = \Drupal::state()->get('uipath_robot.expire');
if ($token && $expire > time()) {
return $token;
}
// 重新登录
$key = \Drupal::service('key.repository')->getKey('uipath_robot_user');
$cred = json_decode($key->getKeyValue(), TRUE);
$auth = \Drupal::httpClient()->post(self::AUTH_URL, [
'json' => [
'tenancyName' => $cred['tenant'],
'username' => $cred['username'],
'password' => $cred['password'],
],
'verify' => TRUE,
]);
$result = json_decode((string)$auth->getBody(), TRUE);
$token = $result['result'];
\Drupal::state()->set('uipath_robot.access_token', $token);
\Drupal::state()->set('uipath_robot.expire', time() + 1800); // 30 min
return $token;
}
}
调用示例:
$jobId = \Drupal::service('uipath_robot.trigger')->start([
'approveId' => $entity->id(),
'filePath' => $file->getFileUri(),
]);
\Drupal::logger('uipath_robot')->info('已提交机器人,JobId=@id', ['@id' => $jobId]);
失败重试:
在 hook_cron() 中扫描自定义队列表,若 Job 状态为“Faulted”或 30 min 无回调,则自动重试,最多 3 次,重试间隔指数退避。
拓展思考
- Headless Drupal 场景:
如果前端是 React 小程序,审批人在手机端点“通过”,Drupal 只做轻量级校验,把任务推给 Orchestrator,机器人跑完后把结果写入 Kafka,Drupal 消费 Kafka 再回写内容状态,实现完全解耦。 - 多云机器人调度:
国内大型集团可能同时存在 UiPath、华为 WeAutomate、阿里云 RPA。Drupal 侧可抽象统一的“RpaDispatcher”接口,用策略模式根据流程编码动态路由到不同平台,降低供应商锁定风险。 - 零信任安全:
在等保 2.0 三级系统中,建议把 Orchestrator 放在 DMZ,Drupal 放在内网,通过 API 网关(如 Kong+国密插件)做双向 mTLS 和流量镜像,网关侧再对接省政务“零信任管控平台”,实现动态授权与分钟级熔断。