You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord Bot GET请求报错:无法获取本地颁发者证书

解决Discord.js中"unable to get local issuer certificate"错误

问题说明

请求Discord API时触发以下错误,核心原因是Node.js无法验证SSL证书的合法性:

HTTPError [FetchError]: request to https://discord.com/api/v9/gateway/bot failed, reason: unable to get local issuer certificate

解决方案

1. 配置信任根证书(推荐生产环境)

  • 从浏览器导出Discord官网的根证书:访问https://discord.com,点击地址栏锁图标 → 证书 → 路径 → 选择根证书 → 导出为PEM格式
  • 启动Bot时指定证书路径:
    NODE_EXTRA_CA_CERTS=./discord-root-cert.pem node your-bot-file.js
    
  • 或在代码中提前设置环境变量:
    process.env.NODE_EXTRA_CA_CERTS = './discord-root-cert.pem';
    const { Client } = require('discord.js');
    

2. 临时禁用证书验证(仅开发环境)

注意:生产环境禁止使用,会导致请求存在安全风险
修改Discord.js客户端的rest配置,关闭证书校验:

const { Client, GatewayIntentBits } = require('discord.js');
const https = require('https');

const client = new Client({
  intents: [GatewayIntentBits.Guilds], // 根据Bot需求添加对应权限
  rest: {
    agent: new https.Agent({ rejectUnauthorized: false })
  }
});

3. 升级Node.js版本

旧版Node.js内置的根证书列表可能不包含Discord使用的证书,升级到最新LTS版本(如18.x/20.x)即可自动解决大部分证书信任问题。

4. 排查代理/防火墙干扰

如果处于公司内网或使用代理:

  • 确保代理服务器的证书已添加到本地信任列表
  • 设置Node.js代理环境变量:
    HTTP_PROXY=http://your-proxy-address:port HTTPS_PROXY=http://your-proxy-address:port node your-bot-file.js
    

内容的提问来源于stack exchange,提问作者Yoda 's

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 18:12:35