> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication/configure-auth-token-mode.md). Agents can also search and read this documentation through its [MCP server](https://docs.convai.com/api-docs/~gitbook/mcp).

# 配置 Auth Token 模式

将 Convai Unity SDK 项目切换到 Auth Token 模式，并在项目设置中为已发布构建配置令牌端点。

配置 Auth Token 模式，使玩家构建版本从你的后端获取短时凭据，而不是随包附带 Convai 账户 API 密钥。如果你已准备好令牌端点，或要直接在 Project Settings 中配置公共令牌端点，请参阅本页。

### 前提条件

* 已保存 API 密钥的 Convai Unity SDK 项目，这样你在配置 Auth Token 模式时，Editor 仍可继续正常工作。
* 后端上用于返回短时 Convai 身份验证令牌的 HTTPS 端点，除非你计划改用基于代码的 `IConvaiAuthTokenProvider` 。

{% hint style="info" %}
如果你的集成使用自定义 `IConvaiAuthTokenProvider` 而不是 Project Settings 中的端点，请仍完成下方的第 1–2 步，以选择 **Auth Token** 作为模式，然后跳过端点字段。请参阅 [编写自定义令牌提供程序](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication/custom-token-provider.md).
{% endhint %}

### 切换到 Auth Token 模式

{% stepper %}
{% step %}

#### 打开 Credentials 部分

在 Unity Editor 菜单栏中，打开 **Convai > Settings** （或 **Edit > Project Settings > Convai SDK**），然后选择 **Credentials** 部分。
{% endstep %}

{% step %}

#### 将 Auth Mode 设为 Auth Token

将 **Auth Mode** 设为 **Auth Token**。Credentials 部分会将 **API Key** 组替换为 Auth Token 配置组。
{% endstep %}

{% step %}

#### 输入 Token Endpoint URL

在 **Token Endpoint URL**中输入后端的令牌端点。端点必须使用 HTTPS，但本地开发期间使用的 HTTP 回环 URL 除外。SDK 会拒绝其他所有非 HTTPS 端点。
{% endstep %}

{% step %}

#### 选择 HTTP 方法

将 **HTTP Method** 设为 **GET** 或 **POST** 以匹配端点所要求的请求方式。
{% endstep %}

{% step %}

#### 确认令牌响应字段

除非你的端点在其他 JSON 字段中返回令牌，否则请将 **Token Response Field** 保留为默认值 `apiAuthToken`。该字段支持点分隔路径，因此像 `data.token` 这样的嵌套字段也能正确解析。
{% endstep %}

{% step %}

#### 如果端点需要请求标头，请添加相应标头

向以下位置添加条目 **请求标头** 仅用于可安全包含在玩家构建中的静态值，例如 API 版本标头。如果端点不需要其他标头，请将此列表留空。
{% endstep %}
{% endstepper %}

{% hint style="warning" %}
项目设置中的标头是静态的，并会随玩家构建一起打包。切勿将玩家令牌、刷新令牌、服务器机密或 Convai API 密钥放入“请求标头”列表中。如果令牌请求需要每位玩家各自的凭据，请改用已注册的 `IConvaiAuthTokenProvider` ——请参阅 [编写自定义令牌提供程序](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication/custom-token-provider.md).
{% endhint %}

### 验证配置

进入播放模式，并使用一个连接到 `ConvaiCharacter`的场景。连接成功即表示端点已在配置的响应字段中返回有效令牌。如果连接失败，请在控制台中查看具体错误消息，并参阅 [排查身份验证问题](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication/troubleshooting.md).

切换到 Auth Token 模式后，已保存的 API 密钥仍可供编辑器工具使用；但在生成 Auth Token 玩家构建期间，两个已存储的密钥字段都会自动清除，并在之后恢复。

### 后续步骤

<table data-view="cards"><thead><tr><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>编写自定义令牌提供程序</strong><br>注册 IConvaiAuthTokenProvider，以提供每位玩家各自的令牌，而不是使用静态端点。</td><td><a href="https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication/custom-token-provider.md">编写自定义令牌提供程序</a></td></tr><tr><td><strong>生成安全的构建</strong><br>了解构建处理器会移除哪些内容，以及令牌端点的 WebGL CORS 要求。</td><td><a href="https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication/ship-a-secure-build.md">发布安全构建</a></td></tr><tr><td><strong>排查身份验证问题</strong><br>两种身份验证模式下的控制台消息和错误代码。</td><td><a href="https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication/troubleshooting.md">身份验证故障排查</a></td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication/configure-auth-token-mode.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
