繁体   English   中英

如何解决 Nexmo Vonage SDK 中的“系统:错误:无效令牌”在同一帐户中更改为新应用程序

[英]How to resolve 'system:error:invalid-token' In Nexmo Vonage SDK changing to a new app in the same account

我在服务器上使用"@vonage/server-sdk": "2.10.7-beta-2" package 在 Vonage 中创建用户。 为了创建用户,我使用了这个 API

const Vonage = require('@vonage/server-sdk');

const v = new Vonage({
 apiKey: config.voipConfig.key,
 apiSecret: config.voipConfig.secret,
 applicationId: config.voipConfig.appId,
 privateKey: config.voipConfig.privateKeyPath
};

v.users.create({
 "name": payload.username,
 "display_name": payload.displayName
}, (error: any, result: any) => {
});

一切正常。 但是当我在 vonage 帐户中创建一个新应用程序并使用新配置时,它开始抛出以下错误

{
      description: 'You did not provide a valid token. Please provide a valid token.',
      code: 'system:error:invalid-token'
    }

我已经检查了很多次细节,不知道出了什么问题。 新凭据来自全新帐户。

任何帮助将非常感激。

看起来您正在使用 Vonage Conversations API创建用户。 在良好的实践中,我们通常使用dotenv来存储我们的.env变量

npm install dotenv

// .env
API_KEY=
API_SECRET=
APPLICATION_ID=
APPLICATION_PRIVATE_KEY_PATH=
TO_NUMBER=<YOUR CELL NUMBER>
VIRTUAL_NUMBER=<VONAGE VIRTUAL NUMBER>
NGROK_URL=https://<URL>.ngrok.io

用于测试目的。 您可以使用下面的代码段拨打电话吗? 这将测试您的凭据。

确保将private.key存储在与.envoutbound-call.js相同的目录中

// outbound-call.js 
require("dotenv").config();
const API_KEY = process.env.API_KEY;
const API_SECRET = process.env.API_SECRET;
const APPLICATION_ID = process.env.APPLICATION_ID;
const APPLICATION_PRIVATE_KEY_PATH = process.env.APPLICATION_PRIVATE_KEY_PATH;
const TO_NUMBER = process.env.TO_NUMBER;
const VIRTUAL_NUMBER = process.env.VIRTUAL_NUMBER;
if (!API_KEY || !API_SECRET) {
  console.log("🔥 API_KEY or API_SECRET missing");
  process.exit(1);
}
if (!APPLICATION_ID || !APPLICATION_PRIVATE_KEY_PATH) {
  console.log("🔥 APPLICATION_ID or APPLICATION_PRIVATE_KEY_PATH missing");
  process.exit(1);
}
if (!TO_NUMBER || !VIRTUAL_NUMBER) {
  console.log("🔥 TO_NUMBER or VIRTUAL_NUMBER missing");
  process.exit(1);
}
const Vonage = require("@vonage/server-sdk");
const vonage = new Vonage({
  apiKey: API_KEY,
  apiSecret: API_SECRET,
  applicationId: APPLICATION_ID,
  privateKey: APPLICATION_PRIVATE_KEY_PATH,
});

vonage.calls.create({
  to: [
    {
      type: "phone",
      number: process.env.TO_NUMBER,
    },
  ],
  from: {
    type: "phone",
    number: process.env.VIRTUAL_NUMBER,
  },
  ncco: [
    {
      action: "talk",
      text: "This is a text to speech call from Vonage",
    },
  ],
});

您可能需要在 Vonage 应用中启用语音功能。 不用担心 webhook,因为您只是打了一个呼出电话。

使用对话 API 时,我通常会这样做:

  1. 创建对话
  2. 创建用户
  3. 创建会员

并将其放入/webhooks/answer

//server.js
require('dotenv').config();
let express = require('express');
let cookieParser = require('cookie-parser');
let logger = require('morgan');
let app = express();
let port = 5001;
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static('public'));

const NGROK_URL = process.env.NGROK_URL;

const Vonage = require('@vonage/server-sdk');
const vonage = new Vonage({
  apiKey: process.env.API_KEY,
  apiSecret: process.env.API_SECRET,
  applicationId: process.env.APPLICATION_ID,
  privateKey: process.env.APPLICATION_PRIVATE_KEY_PATH
});

app.post('/webhooks/answer', async (req, res) => {
  console.log('🚚  answer', req.body);
  let result = req.body;

  const createCallConversationPromise = (ConvName, ConvDisplayName) => new Promise((resolve, reject) => {
    vonage.conversations.create({
        "name": ConvName,
        "display_name": ConvDisplayName,
    }, (error, result) => error ? reject(error) : resolve(result));
  });

  const createCallUserPromise = (Name, DisplayName) => new Promise((resolve, reject) => {
    vonage.users.create({
        name: Name,
        display_name: DisplayName,
    }, (error, result) => error ? reject(error) : resolve(result));
  });

  const createCallMemberPromise = (ConvId, UserId) => {
    vonage.conversations.members.create(ConvId, {
      "action": "join", 
      "user_id": UserId, 
      "channel": {
        "type": "app"
      } 
    }, (error, result) => {
      if(error) {
        console.log('\n🔥 Error Creating Member', error);
      }
      else {
        console.log('\n✅ Created Member with ConvId', ConvId, 'and UserId',UserId)
        console.log(result);
    }
    })
  };

  try {
    let ConvName = result.from + result.to + result.conversation_uuid;
    console.log('n✅ ConvName', ConvName);
    let ConvDisplayName = result.from + result.to + result.conversation_uuid;
    const conversation = await createCallConversationPromise(ConvName, ConvDisplayName);
    process.env.CONVERSATION_ID = conversation.id;
    console.log('\n✅ CONVERSATION_ID', process.env.CONVERSATION_ID);

    let Name = result.from + result.conversation_uuid;
    let DisplayName = result.from;
    const callUser = await createCallUserPromise(Name, DisplayName);
    console.log('\n✅ UserId', callUser.id)

    let ConvId = conversation.id;
    let UserId = callUser.id;
    const memberUser = await createCallMemberPromise(ConvId, UserId);

    let ncco = [
      {
          action: "talk",
          text: "<speak><lang xml:lang='en-GB'>Welcome to Vonage Development inbound call testing</lang></speak>",
          voiceName: "Emma"
      },
    ];
    res.json(ncco);
  
  } catch (error) {
    console.log("🔥 Error", error);
    let ncco = [
      {
          "action": "talk",
          "text": "<speak><lang xml:lang='en-GB'>Error on Process Answer</lang></speak>",
          "voiceName": "Emma"
      }
    ];
    res.json(ncco);
  }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM