Node/Twilio多重配置变量在需要功能

发布于 2025-01-29 08:37:15 字数 853 浏览 3 评论 0原文

我想知道,当使用Twilio作为节点时,是否有人设法找到了一种使用多个帐户SIDS和auth令牌的方法。该文档非常简单,我可以将Twilio与自己的凭据一起使用。

但是,在使用Twilio上的子用户时,我希望能够在购买电话号码的过程中使用其凭据。我目前有一个app.post路由,该路由首先获取特定用户的SID和auth标记。

let twilioSid = process.env.REACT_APP_TWILIO_ACCOUNT_SID;
let twilioAuthToken = process.env.REACT_APP_TWILIO_AUTH_TOKEN;

let twilioClient = require('twilio')(twilioSid, twilioAuthToken);

在进行该号码的实际“购买”之前,我在调用函数之前检索Subuser SID和Auth token并更新我的变量,例如:

const user = await admin.firestore().collection("users").doc(userId).get()
                
twilioSid = user.data().sid;
twilioAuthToken = user.data().authToken;

const purchase = await twilioClient.incomingPhoneNumbers.create({phoneNumber: number})

购买起来,但仅适用于我的主要(父(父)帐户,都存储在.env。似乎最高变量从未在IncoMiingphoneNumbers之前实际进行更新。谁能将我指向我如何使用子用户凭据来运行此功能的正确方向?

I was wondering if any other people have managed to find a way to use multiple account sids and auth tokens when using Twilio for Node. The documentation is pretty straight forward, and I am able to use Twilio with my own credentials.

However, while using subusers on Twilio, I want to be able to use their credentials in the process of purchasing a phone number. I currently have a app.post route which first fetches the sid and auth token of the specific user.

let twilioSid = process.env.REACT_APP_TWILIO_ACCOUNT_SID;
let twilioAuthToken = process.env.REACT_APP_TWILIO_AUTH_TOKEN;

let twilioClient = require('twilio')(twilioSid, twilioAuthToken);

Before doing the actual "purchase" of that number, I retrieve the subuser sid and auth token and update my variable before I call the function, like so:

const user = await admin.firestore().collection("users").doc(userId).get()
                
twilioSid = user.data().sid;
twilioAuthToken = user.data().authToken;

const purchase = await twilioClient.incomingPhoneNumbers.create({phoneNumber: number})

The purchase works, but only for my main (parent) account with the credentials stored in .env. It seems that the top variables never actually gets updated before the incomiingPhoneNumbers.create gets called. Can anyone point me in the right direction on how I would be able to use the subuser credentials to run this function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2025-02-05 08:37:15

更新变量仅在这里无法完成工作,因为您已经初始化了客户端。当您重新启动客户端时(或者只是启动另一个客户)时,它应该起作用:

const user = await admin.firestore().collection("users").doc(userId).get()
                
twilioSid = user.data().sid;
twilioAuthToken = user.data().authToken;

twilioClient = require('twilio')(twilioSid, twilioAuthToken);

const purchase = await twilioClient.incomingPhoneNumbers.create({phoneNumber: number})

const user = await admin.firestore().collection("users").doc(userId).get()
                
twilioSid = user.data().sid;
twilioAuthToken = user.data().authToken;

const userClient = require('twilio')(twilioSid, twilioAuthToken);

const purchase = await userClient.incomingPhoneNumbers.create({phoneNumber: number})

Updating the variables only won't do the job here because you already initialized the client. It should work when you reinitialize the client (or just init another client):

const user = await admin.firestore().collection("users").doc(userId).get()
                
twilioSid = user.data().sid;
twilioAuthToken = user.data().authToken;

twilioClient = require('twilio')(twilioSid, twilioAuthToken);

const purchase = await twilioClient.incomingPhoneNumbers.create({phoneNumber: number})

or

const user = await admin.firestore().collection("users").doc(userId).get()
                
twilioSid = user.data().sid;
twilioAuthToken = user.data().authToken;

const userClient = require('twilio')(twilioSid, twilioAuthToken);

const purchase = await userClient.incomingPhoneNumbers.create({phoneNumber: number})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文