> 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).

# 配置 Auth Token 模式

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

配置 Auth Token 模式，使播放器构建从你的后端获取短期凭证，而不是随构建一起打包 Convai 账户 API 密钥。当你已有可用的令牌端点，或者你正在 Project Settings 中直接配置一个公开的令牌端点时，请使用此页面。

### 前提条件

* 一个已保存 API 密钥的 Convai Unity SDK 项目，这样你在配置 Auth Token 模式时，Editor 仍可继续工作。
* 你后端上的一个 HTTPS 端点，用于返回一个短期 Convai 认证令牌，除非你打算使用基于代码的 `IConvaiAuthTokenProvider` 替代。

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

### 切换到 Auth Token 模式

{% stepper %}
{% step %}

#### 打开凭据部分

在 Unity 编辑器菜单栏中，打开 **Convai > 设置** （或 **编辑 > 项目设置 > Convai SDK**），然后选择 **凭据** 部分。
{% endstep %}

{% step %}

#### 将 Auth Mode 设置为 Auth Token

将 **认证模式** 到 **Auth Token**。Credentials 部分会替代 **API 密钥** 组，改为 Auth Token 配置组。
{% endstep %}

{% step %}

#### 输入令牌端点 URL

在以下位置输入你后端的令牌端点： **令牌端点 URL**。该端点必须使用 HTTPS；本地开发期间使用的 HTTP 回环 URL 除外——SDK 会拒绝任何其他非 HTTPS 端点。
{% endstep %}

{% step %}

#### 选择 HTTP 方法

将 **HTTP 方法** 到 **GET** 或 **POST** 以匹配端点期望的请求方式。
{% endstep %}

{% step %}

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

将 **令牌响应字段** 保持其默认值， `apiAuthToken`，除非你的端点将令牌返回在不同的 JSON 字段中。该字段支持点路径，因此像 `data.token` 这样的嵌套字段也能正确解析。
{% endstep %}

{% step %}

#### 如果你的端点需要请求头，请添加

将条目添加到 **请求头** 仅用于可安全包含在播放器构建中的静态值，例如 API 版本头。如果你的端点不需要额外的请求头，请将此列表留空。
{% endstep %}
{% endstepper %}

{% hint style="warning" %}
Project Settings 中的请求头是静态的，并会随播放器构建一起发布。切勿将玩家令牌、刷新令牌、服务器密钥或 Convai API 密钥放入 Request Headers 列表中。如果你的令牌请求需要按玩家提供的凭证，请改用已注册的 `IConvaiAuthTokenProvider` 来替代——参见 [编写自定义令牌提供器](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication/custom-token-provider.md).
{% endhint %}

### 验证配置

使用一个连接了 `ConvaiCharacter`的场景进入播放模式。连接成功表明端点已在配置的响应字段下返回有效令牌。如果连接失败，请在 Console 中查看确切错误消息，并参见 [排查认证问题](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/authentication/troubleshooting.md).

切换到 Auth Token 模式后，你已保存的 API 密钥仍可供 Editor 工具使用；但在生成 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="/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="/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="/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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
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=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
