Attempting to create a firebase function that is triggered by some database or onRequest event that creates payments on Xero to better manage incoming invoices.
Not sure I understand how to run (if its possible) the xero-node library from firebase functions.
I have been messing around with the two and it doesn't seem to be as simple as initialising the object with the secret ID and calling functions like i would expect:
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')
It looks like I need to generate an Access key with the following (which expires in 12 minutes):
let consentUrl = await xero.buildConsentUrl()
res.redirect(consentUrl)
messing around with the api in postman and this flow feels like i'm making it more complicated than it should be.
Is there a way I can just initialise it with an admin scope or something and not have to worry about access keys... Or should i use xero-node on the client side... (surely not)
There are expressjs versions with xero-node so would i just do the equivilent with firebase onrequest functions by making the redirectUri the address of another firebase function to get the session key?
Im getting somewhere slowly..
my firebase functions look like this:
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)
})
The idea is that I call xeroInit and the token gets sent to xeroAuth which seems to work because console logging the req.url gives:
/?code=c5105f6ce943....230a23fsadf&scope=openid%20profile%20email%20accounting.transactions%20accounting.settings&session_state=tFC6G9Go_zBguCjIpy8-9gl6-9SWLTlUmY5CXMq49es.3c42d9ca9e53285596193bf423f791f3
But i get this error when trying to set it apiCallback
TypeError: Cannot read property 'callbackParams' of undefined
Testing with a pure express app this works perfectly i expect this is a bug with the firebase functions emulator...
Because this worked with express i used express inside my firebase function and calling apiCallback with the request.url worked... Ill update the answer when i get it working fully for others who want to use firebase and xero-node together.
Big thanks to SerKnight who helped me get this working on firebase functions for future reference here is the configuration I got working.
Some notes
xero.initialize()
in a firebase function as a callback didn't work for what ever reason so I used express to handle initial setup.Ensure your firebase function url is put into the
OAuth 2.0 redirect URIs
on xeros myapps page in full as you see it in my example.localhost if using firebase emulators
http://localhost:5001/example/us-central1/xeroInit/connect
token updated
message