> For the complete documentation index, see [llms.txt](https://polyv.gitbook.io/document/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://polyv.gitbook.io/document/docs/live/miniprogram/live-watch-miniprogram-sdk/articles/verify-next/overview.md).

# 授权认证

开发者如果不需要保利威 Saas 的观看条件验证功能时，可通过保利威 Api 接口生成观众授权令牌（以下统称 **授权令牌**），授权令牌验证传入后即可绕过观看条件限制进入直播观看页，详细用法可见以下文档：

## 一、处理流程

### 1.1 步骤1：创建观看核心实例

请参考 [观看页SDK-安装和初始化](https://git.polyv.net/help-center/document-center/-/blob/master/live/miniprogram/live_watch_miniprogram_sdk/articles/product/install/README.md)

### 1.2 步骤2：获取授权令牌

通过保利威 Api 接口获取观众授权令牌，接口文档可见：[获取观看页小程序 SDK 授权令牌](https://help.polyv.net/index.html#/live/api/channel/operate/get_watch_api_token)。

> 由于该接口涉及 appSecret 进行参数签名，建议该步骤由服务端生成。

### 1.3 步骤3：传入授权令牌

通过 **步骤2** 获取授权令牌后，通过核心实例的 `setXAuthToken` 方法传入授权令牌。

### 1.4 步骤4：安装观看核心实例

通过 **步骤3** 传入令牌后即可根据正常流程安装核心实例，注意关于授权令牌的流程仅在**首次安装**前需要，二次安装则不再需要该流程。

## 二、示例代码

以下为 js 方式的流程示例代码：

```js
import { send } from '@just4/ajax/ajax';
import md5 from 'md5';
import { createWatchCore } from '@polyv/live-watch-miniprogram-sdk';

/**
 * 生成签名
 * @param {Object} data 请求参数
 * @param {String} secret 签名密钥
 * @returns 签名串
 */
function getSign(data, secret) {
  let text = '';
  const keys = Object.keys(data).sort();

  for (const key of keys) {
    text += `${key}${data[key]}`;
  }
  const sign = md5(secret + text + secret);
  return sign.toUpperCase();
}

/**
 * 获取授权令牌
 * @param {string} channelId
 */
async function getWatchAuthToken({ channelId }) {
  // 请求数据
  const reqData = {
    appId: '应用 id，从管理后台获取',
    timestamp: Date.now(),
    channelId, // '频道ID',
    viewerId: '观众 id',
    nickname: '观众昵称',
    avatar: '观众头像',
  };
  // 应用密钥，从管理后台获取，建议获取授权令牌流程在服务端中进行，避免将应用密钥直接暴露在 js 中
  const appSecret = 'xxxxx';
  // 生成签名
  const sign = getSign(reqData, appSecret);
  reqData.sign = sign;

  // 通过 ajax 获取授权令牌
  const response = await send('//api.polyv.net/live/v3/channel/watch/get-watch-api-token', {
    method: 'post',
    data: reqData,
  });
  const result = response.data;
  return result.data.token;
}

// 示例方法
async function example() {
  const channelId = '频道号';

  // 步骤1：创建观看核心实例
  const watchCore = createWatchCore({ channelId });

  // 步骤2：获取授权令牌
  const watchAuthToken = await getWatchAuthToken({ channelId });

  // 步骤3：传入授权令牌
  watchCore.setXAuthToken(watchAuthToken);

  // 步骤4：安装观看核心实例
  await watchCore.setup();
}
```


---

# 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:

```
GET https://polyv.gitbook.io/document/docs/live/miniprogram/live-watch-miniprogram-sdk/articles/verify-next/overview.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
