一、环境准备(必做)

  1. Node.js 18+(推荐 22.x)官网
  2. 小红书开放平台账号

    • 完成个人 / 企业认证,创建应用,申请「笔记评论 API」权限。
    • 拿到:App Key、App Secret、Access Token(有效期约 2 小时,需刷新)。
  3. 大模型 API Key(国内推荐阿里云百炼 / DeepSeek,免费额度够用)

二、本地部署 OpenClaw(5 分钟)

1. 一键安装(推荐)

bash

运行

# macOS/Linux 封装好的api添加V试用:18179018113
curl -fsSL https://openclaw.ai/install.sh | bash

# Windows(管理员 PowerShell)
curl -fsSL https://openclaw.ai/install.cmd -o install.cmd && install.cmd && del install.cmd
  • 自动装 Node.js、Git、OpenClaw 本体。

2. 初始化配置

bash

运行

# 启动向导
openclaw onboard

按提示选:

  • 风险提示:Yes
  • 安装模式:QuickStart
  • 模型:选 Custom Provider(后续填国内模型)
  • 网关端口:默认 18789
  • 技能目录:默认 ~/.openclaw/skills

3. 配置模型 API Key(以阿里云百炼为例)

bash

运行

# 设置 API Key
openclaw config set models.providers.custom.apiKey "你的百炼 API Key"
openclaw config set models.providers.custom.baseUrl "https://dashscope.aliyuncs.com/api/v1"
openclaw config set models.providers.custom.model "qwen-turbo"

4. 启动网关

bash

运行

openclaw gateway start
  • 访问控制台:
  • 能聊天回复即部署成功。

三、获取小红书 Access Token 与笔记 ID

1. 获取 Access Token

开放平台 → 应用 → 授权管理 → 生成 Access Token(复制备用)。

2. 获取笔记 ID

从小红书笔记 URL 提取:https://www.xiaohongshu.com/discovery/item/649c46ab000000002702ad36note_id = 649c46ab000000002702ad36


四、编写「小红书评论 API」Skill(核心)

1. 创建技能目录

bash

运行

cd ~/.openclaw/skills
mkdir xhs_comment && cd xhs_comment

2. 编写 main.py

python

运行

import requests
import time
from openclaw.skill import Skill

class XHSCommentSkill(Skill):
    def execute(self, context):
        # 1. 配置(替换为你的信息)
        app_key = "你的小红书 App Key"
        app_secret = "你的 App Secret"
        access_token = "你的 Access Token"
        note_id = context.get("note_id", "649c46ab000000002702ad36")  # 默认测试ID
        page_size = 20
        cursor = ""

        # 2. API 地址
        url = f"https://api.xiaohongshu.com/v2/notes/{note_id}/comments"

        # 3. 签名(按小红书规则)
        timestamp = str(int(time.time()))
        sign_str = f"app_key{app_key}cursor{cursor}note_id{note_id}page_size{page_size}timestamp{timestamp}{app_secret}"
        import hashlib
        sign = hashlib.md5(sign_str.encode()).hexdigest()

        # 4. 请求头
        headers = {
            "Authorization": f"Bearer {access_token}",
            "Content-Type": "application/json"
        }

        # 5. 请求参数
        params = {
            "app_key": app_key,
            "cursor": cursor,
            "page_size": page_size,
            "timestamp": timestamp,
            "sign": sign
        }

        try:
            # 6. 发送请求
            res = requests.get(url, headers=headers, params=params, timeout=15)
            res.raise_for_status()
            data = res.json()

            # 7. 格式化输出
            if data.get("code") != 0:
                return f"❌ 错误:{data.get('msg')}"

            comments = data.get("data", {}).get("comments", [])
            if not comments:
                return "ℹ️ 暂无评论"

            result = f"✅ 共 {len(comments)} 条评论:\n"
            for i, c in enumerate(comments, 1):
                result += f"{i}. {c['user']['nickname']}:{c['content']}\n"
            return result

        except Exception as e:
            return f"❌ 调用失败:{str(e)}"

# 暴露技能实例
skill = XHSCommentSkill()

3. 编写 __init__.py

python

运行

from .main import skill

五、安装依赖并重启网关

bash

运行

# 安装 requests(技能依赖)
pip install requests

# 重启网关加载新技能
openclaw gateway restart

六、调用测试(直接对话)

在 OpenClaw 控制台输入:

plaintext

请调用 xhs_comment 技能获取笔记 649c46ab000000002702ad36 的评论
  • 成功:返回评论列表
  • 失败:提示具体错误(如 Token 过期、签名错误)

七、常见问题排查

  1. 认证失败

    • 检查 App Key/Secret/Token 是否正确、无空格。
    • Token 有效期 2 小时,过期重新生成。
  2. 签名错误

    • 严格按 app_key+cursor+note_id+page_size+timestamp+app_secret 排序拼接。
    • 时间戳用秒级(10 位)。
  3. 技能加载失败

    • 目录必须在 ~/.openclaw/skills
    • 文件名 main.py + __init__.py 缺一不可
    • 重启网关:openclaw gateway restart

八、扩展:支持分页与自动刷新 Token

  • 分页:循环使用 cursor 遍历所有评论。
  • Token 自动刷新:用 OAuth2.0 定时刷新 access_token,避免手动更新。
Logo

小龙虾开发者社区是 CSDN 旗下专注 OpenClaw 生态的官方阵地,聚焦技能开发、插件实践与部署教程,为开发者提供可直接落地的方案、工具与交流平台,助力高效构建与落地 AI 应用

更多推荐