functions
latest
false
函数用户指南
- 概述
- JavaScript 函数
- Python 函数
- 部署并运行
重要 :
请注意,此内容已使用机器翻译进行了本地化。
新发布内容的本地化可能需要 1-2 周的时间才能完成。
函数上下文
运行时传递给每个 JavaScript 函数处理程序的身份、平台坐标和请求数据。
每个处理程序都会收到一个上下文对象作为其第二个参数。它包含函数的调用者、函数自己的平台身份以及出站平台调用的发送位置,因此不必将这些内容作为输入传递。
handler: async (input, ctx) => {
ctx.user?.accessToken // the caller's OAuth token
ctx.user?.sub // the caller's user id
ctx.robot?.accessToken // the function's own platform token
ctx.robot?.key // the serverless robot key
ctx.platform?.baseUrl // e.g. "https://cloud.uipath.com"
ctx.platform?.orgId // organization id
ctx.platform?.tenantId // tenant id
ctx.platform?.folderKey // folder of the invocation, if any
ctx.params // path parameters, as strings
ctx.headers // request headers, lowercase keys
}
handler: async (input, ctx) => {
ctx.user?.accessToken // the caller's OAuth token
ctx.user?.sub // the caller's user id
ctx.robot?.accessToken // the function's own platform token
ctx.robot?.key // the serverless robot key
ctx.platform?.baseUrl // e.g. "https://cloud.uipath.com"
ctx.platform?.orgId // organization id
ctx.platform?.tenantId // tenant id
ctx.platform?.folderKey // folder of the invocation, if any
ctx.params // path parameters, as strings
ctx.headers // request headers, lowercase keys
}
两个身份
一个函数每次调用时都会被赋予两个身份,在这两种身份之间进行选择即为安全决策。
ctx.user | ctx.robot | |
|---|---|---|
| 所有权归属 | 调用函数的调用者 | 函数自己的平台标识 |
| 适用的权限 | 调用者自己的 | 函数的服务帐户 |
| 用于 | 代表已登录的用户执行操作 | 正在读取调用者不得直接访问的资源 |
ctx.user.accessToken 适用于调用者希望仅查看其自身权限允许的内容的情况。当函数必须访问调用者无法访问的内容(例如,受限文件夹中的凭据)时,适用 ctx.robot.accessToken。请参阅访问平台服务。
备注:
ctx.robot 是权限更高的身份。读取请求中到达的任何资源名称的处理程序正在充当调用者请求的代表,具有该函数自身的权限。提取操作必须限制为代码中的固定集合。
平台坐标
ctx.platform 为出站平台调用提供 baseUrl、orgId 和 tenantId。这些来自运行时,而非调用者,因此请求无法重定向函数将其流量发送到的位置,并且绝不会将其接受为输入字段。
这是一个“要么全有,要么全无”的结果:ctx.platform 为 null,除非这三个属性都可用。对于无文件夹调用,folderKey 可以单独为 null。
可用位置
| HTTP 触发器 | 作业 | 本地 serve | |
|---|---|---|---|
ctx.user | 调用者的身份 | 无调用者身份 | 当请求包含持有者令牌时显示 |
ctx.robot | 是 | 是 | null |
ctx.platform | 是 | 是 | 仅适用于设置了 UIPATH_BASE_URL、UIPATH_ORG_ID 和 UIPATH_TENANT_ID 的情况 |
ctx.params, ctx.headers | 是 | 空 — 没有 HTTP 请求 | 是 |
由于 ctx.robot 在本地为 null,因此需要平台令牌的代码应回退到环境变量,以进行本地开发:
const token = ctx.robot?.accessToken || process.env["UIPATH_ACCESS_TOKEN"] || "";
const token = ctx.robot?.accessToken || process.env["UIPATH_ACCESS_TOKEN"] || "";