【发布时间】:2021-01-13 16:15:18
【问题描述】:
尝试创建一个由某些数据库或 onRequest 事件触发的 firebase 函数,该事件在 Xero 上创建付款以更好地管理收到的发票。
不确定我是否了解如何从 firebase 函数运行(如果可能的话)xero-node 库。
我一直在搞乱这两者,它似乎不像用秘密 ID 初始化对象和调用我期望的函数那么简单:
const xero = new XeroClient({
clientId: '230823jff',
clientSecret: 'sdfh39fhsdaf',
redirectUris: [`http://localhost:3000/xero`],
scopes: 'openid profile email accounting.transactions offline_access'.split(' '),
})
xero.accountingApi.getInvoices('tennetid093242')
看来我需要使用以下内容生成访问密钥(12 分钟后到期):
let consentUrl = await xero.buildConsentUrl()
res.redirect(consentUrl)
在邮递员中搞乱 api,这个流程感觉就像我让它变得比它应该的更复杂。
有没有办法我可以用管理员范围或其他东西来初始化它,而不必担心访问密钥...... 或者我应该在客户端使用 xero-node...(肯定不是)
有带有 xero-node 的 expressjs 版本,所以我是否可以通过将 redirectUri 设置为另一个 firebase 函数的地址来获取会话密钥来与 firebase onrequest 函数等效?
我正在慢慢地到达某个地方..
我的 firebase 函数如下所示:
const xero = new XeroClient({
clientId: '123213123123',
clientSecret: '123123123123',
redirectUris: ['http://localhost:5001/example/us-central1/xeroAuthCode'],
scopes: ['openid', 'profile', 'email', 'accounting.transactions', 'accounting.settings', 'offline_access']
})
exports.xeroInit = functions.https.onRequest(async (req, res) => {
let consentUrl = await xero.buildConsentUrl()
res.redirect(consentUrl)
})
exports.xeroAuthCode = functions.https.onRequest(async (req, res) => {
let tokenSet = await xero.apiCallback(req.url)
console.log(tokenSet)
})
我的想法是我调用 xeroInit 并将令牌发送到 xeroAuth 这似乎有效,因为控制台记录 req.url 给出:
/?code=c5105f6ce943....230a23fsadf&scope=openid%20profile%20email%20accounting.transactions%20accounting.settings&session_state=tFC6G9Go_zBguCjIpy8-9gl6-9SWLTlUmY5CXMq49es.3c42d9ca9e53285596193bf423f791f3
但我在尝试设置 apiCallback 时收到此错误
TypeError: Cannot read property 'callbackParams' of undefined
使用纯 express 应用程序进行测试,效果很好,我认为这是 firebase 函数模拟器的错误...
因为这与 express 一起使用,所以我在我的 firebase 函数中使用 express 并使用 request.url 调用 apiCallback 工作...... 当我让它完全适用于想要一起使用 firebase 和 xero-node 的其他人时,我会更新答案。
【问题讨论】:
-
编辑了我的答案我认为你只需要重做 -> await xero.initialize() 它看起来与github.com/XeroAPI/xero-node/issues/298相同的问题
标签: node.js firebase google-cloud-platform google-cloud-functions xero-api