使用外部应用程序访问 UiPath 资源
这些说明适用于在具有内部部署 Orchestrator 安装或自托管 Orchestrator 安装的环境中维护 UiPath 产品与外部应用程序之间集成的开发者。
在开始之前:
- 外部应用程序必须已由组织管理员在“ 外部应用程序 ”中注册。
- 从组织管理员处获取注册详细信息。
在注册外部应用程序后,您必须为外部应用程序实施适当的授权机制,并为允许的作用域提供适当的授权类型,以便外部应用程序可以检索访问令牌。
要使用的授权类型:
如果同时向用户和应用程序作用域授予了一个机密应用程序,则您必须同时实现这两种授予类型。
当用户和应用程序作用域内的作用域名称相同时(如在 Orchestrator 中),您使用的授予类型将决定是在用户作用域还是应用程序作用域下调用资源。
用于访问 UiPath 资源的授权服务器是 UiPath Identity Server,它支持 OAuth 2.0 框架。
如果注册的应用程序是非机密类型且请求是针对用户作用域,则请使用此授权类型。
流程与使用授权代码时相同,不同之处为在授权请求中,您需要包括以下请求查询参数:
code_challenge_method
,必须为S256
code_challenge
,这是从 code_verifier 派生的加密随机字符串,用于将授权请求连接到令牌请求。
您必须使用代码验证程序算法来生成代码质询。您也可以创建自己的算法,但前提是它必须符合 rfc7636 标准。
{BaseURL}/identity/connect/authorize?
response_type=code
&client_id={app_id}
&scope={scopes}
&redirect_uri={redirect_url}
&code_challenge={cryptographically-random string from code_verifier}
&code_challenge_method=S256
{BaseURL}/identity/connect/authorize?
response_type=code
&client_id={app_id}
&scope={scopes}
&redirect_uri={redirect_url}
&code_challenge={cryptographically-random string from code_verifier}
&code_challenge_method=S256
{BaseURL}/identity/connect/token
的 POST 令牌请求中,您需要在请求正文中包含 code_verifier
(用于生成 code_challenge
的原始字符串)。
application/x-www-form-urlencoded
。
{
grant_type: "authorization_code"
code: "{authorization_code}"
redirect_uri: "{redirect_url}"
client_id: "{app_id}"
code_verifier: "{code_verifier}"
}
{
grant_type: "authorization_code"
code: "{authorization_code}"
redirect_uri: "{redirect_url}"
client_id: "{app_id}"
code_verifier: "{code_verifier}"
}
client_id
和 client_secret
的 POST 请求 {BaseURL}/identity/connect/token
,以请求访问令牌。
application/x-www-form-urlencoded
。
{
grant_type: "client_credentials"
client_id: "{app_id}"
client_secret: "{app_secret}"
scope: "{scopes}"
}
{
grant_type: "client_credentials"
client_id: "{app_id}"
client_secret: "{app_secret}"
scope: "{scopes}"
}
应用程序获得访问令牌后,它可以使用令牌来访问允许的资源(仅限于选定的作用域),直到令牌过期(一小时)。
这是向 odata/Machines API 发出的一个示例请求,该请求在“授权”标头中使用访问令牌,其中访问令牌的作用域中包含“OR.Machines 作用域”。
curl -X GET "{OrchestratorURL}/odata/Machines"
-H "Authorization: Bearer {access_token}" -H "accept: application/json"l
curl -X GET "{OrchestratorURL}/odata/Machines"
-H "Authorization: Bearer {access_token}" -H "accept: application/json"l
{OrchestratorURL}/swagger/index.html
上的 Orchestrator API Swagger。
访问令牌将在一小时后过期。外部应用程序可以通过交换刷新令牌来获取新的访问令牌,而无需用户交互。
刷新令牌也仅能使用一次,并且会在 60 天后过期。
scope
参数中包含 offline_access
,以便可以在令牌请求中使用授权代码来获取刷新令牌。
要获取刷新令牌,请将包含授权代码的 POST 请求发送到令牌端点
{BaseURL}/identity/connect/token
。
application/x-www-form-urlencoded
。
该请求将返回一个新的访问令牌和一个刷新令牌:
{
"access_token": "{access_token}",
"expires_in": 3600,
"token_type": "Bearer",
"refresh_token": "{refresh_token}",
"scope": "OR.Machines OR.Robots offline_access"
}
{
"access_token": "{access_token}",
"expires_in": 3600,
"token_type": "Bearer",
"refresh_token": "{refresh_token}",
"scope": "OR.Machines OR.Robots offline_access"
}
refresh_token
授予类型向令牌端点 {BaseURL}/identity/connect/token
发送 POST 请求。
application/x-www-form-urlencoded
。
{
grant_type: "refresh_token"
client_id: "{app_id}"
client_secret: "{app_secret}"
refresh_token: "{existing_refresh_token}"
}
{
grant_type: "refresh_token"
client_id: "{app_id}"
client_secret: "{app_secret}"
refresh_token: "{existing_refresh_token}"
}
该响应将返回一个新的访问令牌和一个新的刷新令牌。
现有刷新令牌在使用后不再有效,因此请确保使用收到的新刷新令牌。