# 用户鉴权 (/zh-CN/api-reference/rtm/ios/token)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

鉴权是指在用户访问你的系统前，对其进行身份校验。用户在使用声网服务，如加入音视频通话或登录信令系统时，声网使用 Token 对其鉴权。

RTM 提供 Message Channel、User Channel 和 Stream Channel 三种频道类型，使用不同类型的频道，对应的 Token 类型也不同：

- 使用 Message Channel 或 User Channel：只需在调用 `loginByToken` 方法登录 RTM 系统时，传入开通了 RTM 服务的 Token（以下简称 RTM Token）。
- 使用 Stream Channel：在使用 RTM Token 鉴权的基础上，还需要在调用 `joinWithOption` 方法加入 Stream Channel 时传入开通了 RTC 服务的 Token（以下简称 RTC Token）。

Token 的有效期最长为 24 小时，声网推荐你在 Token 过期前更新 Token。本文介绍如何更新 Token。

想要了解生成和使用 Token 的更多信息，可以查看：

- [用户鉴权](/zh-CN/realtime-media/rtm/build/authentication-and-connection/user-authentication)

## AgoraRtmClientKit.renewToken [#agorartmclientkitrenewtoken]

### 接口描述 [#接口描述]

调用 `AgoraRtmClientKit.renewToken` 方法更新 RTM Token。

为保证及时更新 Token，声网推荐你监听 `tokenPrivilegeWillExpire` 回调，详见[事件监听](/zh-CN/api-reference/rtm/ios/configuration#事件监听)。成功添加事件监听后，当 RTM Token 还有 30 秒过期时，SDK 会触发 `tokenPrivilegeWillExpire` 回调，提醒用户 Token 即将过期。收到该回调时，你可以在服务端重新生成 RTM Token，然后调用 `renewToken` 方法，将新生成的 RTM Token 传给 SDK。

### 接口方法 [#接口方法]

你可以通过以下方式调用 `AgoraRtmClientKit.renewToken` 方法：

```objc
- (void) renewToken:(NSString* _Nonnull)token
         completion:(AgoraRtmOperationBlock _Nullable)completionBlock NS_SWIFT_NAME(renewToken(_:completion:));
```

| 参数 | 类型 | 是否必填 | 默认值 | 描述 |
| --- | --- | --- | --- | --- |
| `token` | string | 必填 | - | 新生成的 RTM Token。 |
| `completion` | `AgoraRtmOperationBlock` | 选填 | - | [token-ios-html-0-2-4 details](#table-slot-token-ios-html-0-2-4) |

<a id="table-slot-token-ios-html-0-2-4"></a>

**token-ios-html-0-2-4 details**

调用结果回调：

- 调用成功：在 `response` 参数中返回 `AgoraRtmCommonResponse` 数据，在 `errorInfo` 参数中返回 `nil`。
- 调用失败：在 `response` 参数中返回 `nil`，在 `errorInfo` 参数中返回错误信息。

### 基本用法 [#基本用法]

```objc
[rtm renewToken:@"new token" completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
    if (errorInfo == nil) {
        NSLog(@"renew token success!!");
    } else {
        NSLog(@"renew token failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
    }
}];
```

## AgoraRtmStreamChannel.renewToken [#agorartmstreamchannelrenewtoken]

### 接口描述 [#接口描述-1]

调用 `AgoraRtmStreamChannel.renewToken` 方法更新 RTC Token。

为保证及时更新 Token，声网推荐你监听 `tokenPrivilegeWillExpire` 回调，详见[事件监听](/zh-CN/api-reference/rtm/ios/configuration#事件监听)。成功添加事件监听后，当 RTC Token 还有 30 秒过期时，SDK 会触发 `tokenPrivilegeWillExpire` 回调，提醒用户 Token 即将过期。收到该回调时，你可以在服务端重新生成 RTC Token，然后调用 `renewToken` 方法，将新生成的 RTC Token 传给 SDK。

### 接口方法 [#接口方法-1]

你可以通过以下方式调用 `AgoraRtmStreamChannel.renewToken` 方法：

```objc
- (void)renewToken:(NSString* _Nonnull)token
        completion:(AgoraRtmOperationBlock _Nullable)completionBlock NS_SWIFT_NAME(renewToken(_:completion:));
```

| 参数 | 类型 | 是否必填 | 默认值 | 描述 |
| --- | --- | --- | --- | --- |
| `token` | string | 必填 | - | 新生成的 RTC Token。 |
| `completion` | `AgoraRtmOperationBlock` | 选填 | - | [token-ios-html-1-2-4 details](#table-slot-token-ios-html-1-2-4) |

<a id="table-slot-token-ios-html-1-2-4"></a>

**token-ios-html-1-2-4 details**

调用结果回调：

- 调用成功：在 `response` 参数中返回 `AgoraRtmCommonResponse` 数据，在 `errorInfo` 参数中返回 `nil`。
- 调用失败：在 `response` 参数中返回 `nil`，在 `errorInfo` 参数中返回错误信息。

### 基本用法 [#基本用法-1]

```objc
[streamChannel renewToken:@"new token" completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
    if (errorInfo == nil) {
        NSLog(@"renew token success!!");
    } else {
        NSLog(@"renew token failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
    }
}];
```
