admin管理员组

文章数量:1134744

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('', {
    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!

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!

Share Improve this question asked Jan 7 at 21:23 Stevan NajeebStevan Najeeb 1092 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Sometimes, the issue might be on QuickBooks' side. You could check the QuickBooks Developer API status page or look for any updates in forums to see if there are any ongoing problems with their OAuth service.

If you've already checked the variables and the request format, try adding more detailed logging. Focus on logging the full response from QuickBooks, as it might give you more information about the error and help you figure out what’s going wrong.

本文标签: nodejsQuickBooks oAuth2 Get Tokens With Auth Code (NodeJs)Stack Overflow