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

使用Twilio Voice JavaScript SDK启动设备报错:identity未在选项中指定

问题描述

使用GitHub上的Twilio Voice JavaScript SDK结合ngrok开发,安装流程正常,但点击启动设备时无法获取客户端名称或发起呼叫。已在index.js中配置account sid、电话号码、twiml app sid及Twilio api secret key,出现以下报错:

CMD报错

Error: identity is required to be specified in options
    at new AccessToken (C:\xampp\htdocs\voice-javascript-sdk-quickstart-node\node_modules\twilio\lib\jwt\AccessToken.js:202:19)
    at tokenGenerator (C:\xampp\htdocs\voice-javascript-sdk-quickstart-node\src\handler.js:13:23)
    at C:\xampp\htdocs\voice-javascript-sdk-quickstart-node\src\router.js:7:12
    at Layer.handle [as handle_request] (C:\xampp\htdocs\voice-javascript-sdk-quickstart-node\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\xampp\htdocs\voice-javascript-sdk-quickstart-node\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (C:\xampp\htdocs\voice-javascript-sdk-quickstart-node\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (C:\xampp\htdocs\voice-javascript-sdk-quickstart-node\node_modules\express\lib\router\layer.js:95:5)
    at C:\xampp\htdocs\voice-javascript-sdk-quickstart-node\node_modules\express\lib\router\index.js:284:15
    at Function.process_params (C:\xampp\htdocs\voice-javascript-sdk-quickstart-node\node_modules\express\lib\router\index.js:346:12)
    at next (C:\xampp\htdocs\voice-javascript-sdk-quickstart-node\node_modules\express\lib\router\index.js:280:10)

ngrok请求日志(点击启动设备时)

HTTP Requests
-------------

GET /quickstart.js             200 OK
GET /twilio.min.js             200 OK
GET /site.css                  200 OK
GET /                          200 OK
GET /                          502 Bad Gateway

config.js代码

const dotenv = require("dotenv");
const cfg = {};

if (process.env.NODE_ENV !== "test") {
  dotenv.config({ path: ".env" });
} else {
  dotenv.config({ path: ".env.example", silent: true });
}

// HTTP Port to run our web application
cfg.port = process.env.PORT || 3000;

// Your Twilio account SID and auth token, both found at:
// https://www.twilio.com/user/account
//
// A good practice is to store these string values as system environment
// variables, and load them from there as we are doing below. Alternately,
// you could hard code these values here as strings.
cfg.accountSid = process.env.TWILIO_ACCOUNT_SID;

cfg.twimlAppSid = process.env.TWILIO_TWIML_APP_SID;
cfg.callerId = process.env.TWILIO_CALLER_ID;

cfg.apiKey = process.env.TWILIO_API_KEY;
cfg.apiSecret = process.env.TWILIO_API_SECRET;

// Export configuration object
module.exports = cfg;
解决方案

1. 报错根源

CMD中的错误明确指出:生成Twilio AccessToken时必须指定identity参数,后端生成token的逻辑未传入该必填项,导致请求失败,进而引发ngrok的502错误。

2. 修复步骤

步骤1:确保前端传递identity参数

打开前端的quickstart.js,找到获取token的请求代码,修改为传递客户端名称作为identity参数:

// 获取输入框中的客户端名称
const identity = document.getElementById('client-name').value;
// 发起请求时携带identity参数
fetch(`/token?identity=${encodeURIComponent(identity)}`)
  .then(response => response.json())
  .then(data => {
    // 后续处理逻辑
  });

步骤2:修改后端token生成逻辑

打开src/handler.js,更新tokenGenerator函数,从请求中获取identity并传入AccessToken:

function tokenGenerator(req, res) {
  const AccessToken = twilio.jwt.AccessToken;
  const VoiceGrant = AccessToken.VoiceGrant;

  // 从请求URL的query参数中获取identity
  const identity = req.query.identity;
  if (!identity) {
    return res.status(400).send('identity参数不能为空');
  }

  // 创建AccessToken时必须传入identity选项
  const token = new AccessToken(
    cfg.accountSid,
    cfg.apiKey,
    cfg.apiSecret,
    { identity: identity }
  );

  const voiceGrant = new VoiceGrant({
    outgoingApplicationSid: cfg.twimlAppSid,
    incomingAllow: true,
  });
  token.addGrant(voiceGrant);

  res.send({ token: token.toJwt() });
}

步骤3:验证.env配置

检查项目根目录的.env文件,确保所有Twilio配置项填写正确,无拼写错误或遗漏:

TWILIO_ACCOUNT_SID=你的账号SID
TWILIO_TWIML_APP_SID=你的TWIML应用SID
TWILIO_CALLER_ID=你的Twilio电话号码
TWILIO_API_KEY=你的API Key
TWILIO_API_SECRET=你的API Secret

步骤4:重启服务和ngrok

  • 停止当前运行的Node.js服务,重新启动:npm start
  • 停止ngrok后重新启动,确保映射端口与服务端口一致(默认3000):ngrok http 3000

3. 验证结果

完成上述修改后,点击启动设备时,前端会将客户端名称作为identity传递给后端,后端生成包含正确identity的AccessToken,即可正常获取token并发起呼叫,ngrok的502错误也会消失。

内容的提问来源于stack exchange,提问作者Abdellah Hajjam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 10:55:50