Reputation: 109
I am having trouble setting up my function to get my auth tokens from quickbooks using the auth code.
cors(req, res, async () => {
const clientId = functions.config().quickbooks.client_id;
const clientSecret = functions.config().quickbooks.client_secret;
const redirectUri = functions.config().quickbooks.redirect_uri;
const basicAuth = require('btoa')(clientId + ':' + clientSecret)
const { code } = req.body;
if (!code) {
return res.status(400).send({ error: 'Authorization code is required' });
}
// Prepare the request body (URL encoded)
const requestBody = new URLSearchParams({
grant_type: 'authorization_code',
code: code,
redirect_uri: redirectUri,
}).toString();
try {
const response = await fetch('https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${basicAuth}`,
'Accept': 'application/json',
},
body: requestBody,
});
// Check if response is OK, else log the error
if (!response.ok) {
const errorDetails = await response.text(); // Capture non-JSON response
console.error("QuickBooks API Error:", errorDetails);
return res.status(500).send({ error: 'Error authenticating with QuickBooks', details: errorDetails });
}
const data = await response.json();
return res.status(200).send({ success: true, data: data });
} catch (error) {
console.error("Error in exchangeAuthCodeForTokens function:", error.message);
return res.status(500).send({ error: error.message });
}
});
I am always getting status 500 (Internal Server Error)
I have console-logged all variables, and they match, with no other errors found.
What could I be doing here that could cause the issues at hand? Any help would be great, thanks!
Upvotes: 0
Views: 34