简体   繁体   English

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

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

I am using "@vonage/server-sdk": "2.10.7-beta-2" package on server to create users in Vonage.我在服务器上使用"@vonage/server-sdk": "2.10.7-beta-2" package 在 Vonage 中创建用户。 To create the user, I used this API为了创建用户,我使用了这个 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) => {
});

Everything was working fine.一切正常。 But when I created a new application in vonage account and used new configs, it started to throw the following error但是当我在 vonage 帐户中创建一个新应用程序并使用新配置时,它开始抛出以下错误

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

I have checked the details multiple times and not sure what is wrong.我已经检查了很多次细节,不知道出了什么问题。 The new credentials are from completely new account.新凭据来自全新帐户。

Any help would be much appreciated.任何帮助将非常感激。

Looks like you are using the Vonage Conversations API to create a user.看起来您正在使用 Vonage Conversations API创建用户。 In good practice, we usually use dotenv to store our .env variables在良好的实践中,我们通常使用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

For testing purposes.用于测试目的。 Can you make an outbound call with the snippet below?您可以使用下面的代码段拨打电话吗? That'll test your credentials.这将测试您的凭据。

Make sure to store the private.key in same directory as .env and outbound-call.js确保将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",
    },
  ],
});

You may need to enable Voice capability in your Vonage App.您可能需要在 Vonage 应用中启用语音功能。 Don't worry about webhooks since your just making an outbound call.不用担心 webhook,因为您只是打了一个呼出电话。

When working with Conversations API, I usually do:使用对话 API 时,我通常会这样做:

  1. Create a Conversation创建对话
  2. Create a User创建用户
  3. Create a Member创建会员

and put that into the /webhooks/answer并将其放入/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.

相关问题 如何使用 Nexmo/Vonage 发送群发短信 - How to send bulk SMS using Nexmo/Vonage 如何解决 Nodejs google-auth-library 无效令牌签名错误? - How to resolve Nodejs google-auth-library invalid token signature error? 获取服务器到服务器的访问权限(Google服务帐户)令牌。 invalid_grant错误 - Getting a server to server access (google service account) token. invalid_grant error 如何解决“Plaid API:无法获取 link_token”错误 - How to resolve the "Plaid API: Unable to fetch link_token" error 如何解决Reactjs / Express应用程序中的代理错误? - How to resolve a proxy error in a reactjs/ express app? 错误:“您必须提供应用访问令牌,或作为应用所有者或开发者的用户访问令牌”使用 Facebook 登录 SDK - Error: "You must provide an app access token, or a user access token that is an owner or developer of the app" using Facebook Login SDK 如何解决 nodemailer 和 gmail 服务的“invalid_grant”错误? - How to resolve "invalid_grant" error with nodemailer and gmail service? 错误:无效的令牌签名:token_here - Error: Invalid token signature: token_here 错误 [TOKEN_INVALID],将有效令牌变为无效令牌 - Error [TOKEN_INVALID], Turns valid token into invalid token 如何解决nodejs中的“错误:无效的报告者“html-cov”” - How resolve "Error: invalid reporter "html-cov"" in nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM