

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 为 Confluence 设置 OAuth 2.0 身份验证
<a name="kb-managed-confluence-oauth2-setup"></a>

OAuth 2.0 身份验证 (`OAUTH2`) 使用 Atlassian OAuth 2.0 三足式 (3LO) 应用程序进行身份验证。您可以在 Atlassian 开发者控制台中注册应用程序，完成一次性授权流程以获取刷新令牌，然后将凭据存储在中。 AWS在抓取时，Amazon Bedrock 会使用刷新令牌自动铸造短期访问令牌。

**重要**  
OAuth 2.0 不支持文档级访问控制 (ACL)。要按用户权限筛选查询结果，请使用基本身份验证。请参阅[为 Confluence 设置基本身份验证](kb-managed-confluence-basic-setup.md)。

## 第 1 步：注册 OAuth 2.0 (3LO) 应用程序
<a name="kb-managed-confluence-oauth2-step1"></a>

1. 登录 [Atlassian 开发者控制台](https://developer.atlassian.com/console/myapps/)。

1. 选择**创建**，然后选择 **OAuth 2.0 集成。**

1. 输入应用程序的名称（例如`bedrock-confluence-connector`），然后选择**创建**。

1. 在应用程序的**权限**页面上，添加 **Confluence** API。配置以下精细范围（如果您的应用使用这些范围，则配置等效的经典范围）：
   + `read:content:confluence`
   + `read:content-details:confluence`
   + `read:space:confluence`
   + `read:space-details:confluence`
   + `read:user:confluence`
   + `read:attachment:confluence`
   + `read:page:confluence`— 页面枚举所必需的
   + `read:blogpost:confluence`— 如果您抓取博客文章，则为必填项
   + `read:custom-content:confluence`— 如果您的工作区包含自定义内容，则为必填项

   添加`offline_access`**授权**-**用户身份 API** 权限。 `offline_access`是导致 Atlassian 发出刷新令牌的原因。

1. 在**授权**页面上，将**回调 URL** 设置为您可以控制的 URL，您可以在其中捕获步骤 3 中的授权码（例如`https://localhost:8080/callback`）。

1. 在 **“设置”** 页面上，复制**客户端 ID** 和**密钥**。这些是`confluenceAppKey`和`confluenceAppSecret`。

## 第 2 步：对应用程序进行授权
<a name="kb-managed-confluence-oauth2-step2"></a>

在浏览器中打开以下 URL，用您的值替换占位符。使用连接器应使用其访问权限的 Atlassian 用户登录；该用户必须有权访问您要抓取的 Confluence 内容。

```
https://auth.atlassian.com/authorize?
audience=api.atlassian.com&
client_id={{your-client-id}}&
scope=read:content:confluence%20read:content-details:confluence%20read:space:confluence%20read:space-details:confluence%20read:user:confluence%20read:attachment:confluence%20read:page:confluence%20read:blogpost:confluence%20read:custom-content:confluence%20offline_access&
redirect_uri={{https://localhost:8080/callback}}&
response_type=code&
prompt=consent
```

批准同意提示。Atlassian 使用查询参数重定向到您的回传网址。`code`复制该授权码。

## 第 3 步：用代码兑换代币
<a name="kb-managed-confluence-oauth2-step3"></a>

使用授权码兑换访问令牌和刷新令牌。在终端上运行：

```
curl -X POST https://auth.atlassian.com/oauth/token \
  -H "Content-Type: application/json" \
  -d '{
    "grant_type": "authorization_code",
    "client_id": "{{your-client-id}}",
    "client_secret": "{{your-client-secret}}",
    "code": "{{authorization-code-from-step-2}}",
    "redirect_uri": "{{https://localhost:8080/callback}}"
  }'
```

响应中包含`access_token`和`refresh_token`。记录两个值。只要定期使用，刷新令牌就不会过期。

## 第 4 步：创建 Secrets Manager 密钥
<a name="kb-managed-confluence-oauth2-step4"></a>

使用以下键值对将凭证存储在 AWS Secrets Manager 密钥中：

```
{
    "confluenceAppKey": "{{your-client-id}}",
    "confluenceAppSecret": "{{your-client-secret}}",
    "confluenceAccessToken": "{{your-access-token}}",
    "confluenceRefreshToken": "{{your-refresh-token}}",
    "hostUrl": "{{https://your-instance.atlassian.net}}"
}
```

使用以下命令创建密钥 AWS Command Line Interface：

```
aws secretsmanager create-secret \
  --name {{bedrock-confluence-oauth2-creds}} \
  --secret-string file://secret.json
```

记录响应中的秘密 ARN。您可以将其用作数据源`secretArn`。

## 步骤 5：向服务角色授予更新密钥的权限
<a name="kb-managed-confluence-oauth2-step5"></a>

Atlassian 访问令牌将在 60 分钟后过期。Amazon Bedrock 使用刷新令牌创建新的访问令牌，并将新值写回同一个密钥。将密钥添加到`secretsmanager:PutSecretValue`知识库服务角色的权限策略中：

```
{
    "Effect": "Allow",
    "Action": "secretsmanager:PutSecretValue",
    "Resource": "{{arn:aws:secretsmanager:region:account-id:secret:bedrock-confluence-oauth2-creds-*}}"
}
```

如果没有此权限，连接器就无法保留轮换的访问令牌，后续同步将失败。

## 后续步骤
<a name="kb-managed-confluence-oauth2-next"></a>

存储密钥后，在`authType`设置为的情况下创建数据源`OAUTH2`。请参阅[连接 Confluence 数据源](kb-managed-ds-confluence-connect.md)。