如何从UIPath自动获取Authorization Code 满足脚本自动化运行要求
解决方案:切换为Client Credentials授权模式
你当前使用的授权码流本身设计为需要用户交互的场景,无法实现全自动化调用。要实现无人工干预的机器人启动逻辑,直接使用UiPath官方提供的机器对机器(M2M)客户端凭据授权模式即可,全程不需要获取authorization code,直接用客户端凭证兑换access token调用接口。
1. Orchestrator前置配置
- 登录你的UiPath Orchestrator后台,进入「Admin」-「External Applications」
- 点击「Add Application」,选择「Machine to Machine Application」类型
- 配置应用名称,给该应用分配所需权限(如需启动机器人,至少要授予
Jobs - Create、Robots - View、Processes - View等对应权限) - 配置完成后保存,记录生成的
Client ID、Client Secret,以及你的租户Logical Name、所属区域(比如美区是cloud、欧盟区是eu、亚太区是au等)
2. 可直接运行的curl示例
将以下命令中的占位符替换为你自己的配置,运行即可直接拿到调用接口需要的access token:
curl --location 'https://<区域>.uipath.com/identity_/connect/token' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'grant_type=client_credentials' \ --data-urlencode 'client_id=<你的Client ID>' \ --data-urlencode 'client_secret=<你的Client Secret>' \ --data-urlencode 'scope=OR.Jobs OR.Robots OR.Processes' \ --data-urlencode 'audience=https://<区域>.uipath.com/api/'
返回结果中的access_token有效期通常为1小时,失效后重新调用该接口即可刷新凭证。
3. 适配Google Appscript的代码示例
你可以直接将这段代码整合到现有脚本中使用:
function getUiPathAccessToken() { // 替换为你自己的配置参数 const config = { region: "你的区域,例如cloud/eu/au", clientId: "你的Client ID", clientSecret: "你的Client Secret", scope: "OR.Jobs OR.Robots OR.Processes" } const tokenUrl = `https://${config.region}.uipath.com/identity_/connect/token`; const payload = { grant_type: "client_credentials", client_id: config.clientId, client_secret: config.clientSecret, scope: config.scope, audience: `https://${config.region}.uipath.com/api/` }; const options = { method: "POST", contentType: "application/x-www-form-urlencoded", payload: payload, muteHttpExceptions: true }; const response = UrlFetchApp.fetch(tokenUrl, options); const tokenData = JSON.parse(response.getContentText()); return tokenData.access_token; } // 调用示例:启动指定机器人任务 function startUiPathJob() { const accessToken = getUiPathAccessToken(); // 替换为你自己的Orchestrator启动任务接口地址 const startJobUrl = "https://<你的区域>.uipath.com/<你的租户逻辑名>/<你的组织名>/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs"; const payload = { "startInfo": { "ReleaseKey": "你要启动的流程对应的ReleaseKey", "Strategy": "Specific", "RobotIds": [对应机器人的数字ID], "InputArguments": JSON.stringify({}) // 无入参则留空 } }; const options = { method: "POST", contentType: "application/json", headers: { "Authorization": `Bearer ${accessToken}` }, payload: JSON.stringify(payload), muteHttpExceptions: true }; const response = UrlFetchApp.fetch(startJobUrl, options); console.log(response.getContentText()); }
内容的提问来源于stack exchange,提问作者Phil
相关产品推荐
相关产品推荐

