Last active
January 28, 2024 11:36
-
-
Save goelp/945ee0583e1df9663cc9e17ae5a2b9bb to your computer and use it in GitHub Desktop.
Google Apps Script OAuth2 script for QuickBooks integration with Google Sheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CLIENT_ID = '...'; // Get from Quickbooks Developer Console | |
var CLIENT_SECRET = '...'; // Get from Quickbooks Developer Console | |
var BASE_AUTH_URL = 'https://appcenter.intuit.com/connect/oauth2'; | |
var TOKEN_URL = 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer'; | |
var API_SCOPE = 'com.intuit.quickbooks.accounting'; | |
var REDIRECT_URI = '...'; // Generate using the logRedirectUri() function mentioned at the end of this script | |
var RESPONSE_TYPE = 'code'; | |
/** | |
* Authorizes and makes a request to the Quickbooks API using OAuth 2. | |
*/ | |
function run() { | |
var service = getService(); | |
if (service.hasAccess()) { | |
var url = 'https://sandbox-quickbooks.api.intuit.com/'; | |
var response = UrlFetchApp.fetch(url, { | |
headers: { | |
Authorization: 'Bearer ' + service.getAccessToken() | |
} | |
}); | |
var result = JSON.parse(response.getContentText()); | |
Logger.log(JSON.stringify(result, null, 2)); | |
} else { | |
var authorizationUrl = service.getAuthorizationUrl(); | |
Logger.log('Open the following URL and re-run the script: %s', authorizationUrl); | |
} | |
} | |
/** | |
* Reset the authorization state, so that it can be re-tested. | |
*/ | |
function reset() { | |
getService().reset(); | |
} | |
/** | |
* Configures the service. | |
*/ | |
function getService() { | |
return OAuth2.createService('Quickbooks') | |
.setAuthorizationBaseUrl(BASE_AUTH_URL) | |
.setTokenUrl(TOKEN_URL) | |
.setClientId(CLIENT_ID) | |
.setClientSecret(CLIENT_SECRET) | |
.setScope(API_SCOPE) | |
.setCallbackFunction('authCallback') | |
.setParam('response_type', RESPONSE_TYPE) | |
.setParam('state', getStateToken('authCallback')) // function to generate the state token on the fly | |
.setPropertyStore(PropertiesService.getUserProperties()); | |
} | |
/** | |
* Handles the OAuth callback | |
*/ | |
function authCallback(request) { | |
var service = getService(); | |
var authorized = service.handleCallback(request); | |
if (authorized) { | |
Logger.log("Success!"); | |
return HtmlService.createHtmlOutput('Success!'); | |
} else { | |
Logger.log("Denied!"); | |
return HtmlService.createHtmlOutput('Denied.'); | |
} | |
} | |
/** | |
* Generate a State Token | |
*/ | |
function getStateToken(callbackFunction){ | |
var stateToken = ScriptApp.newStateToken() | |
.withMethod(callbackFunction) | |
.withTimeout(120) | |
.createToken(); | |
return stateToken; | |
} | |
/** | |
* Logs the redirect URI. Run this function to get the REDIRECT_URI to be mentioned at the top of this script. | |
*/ | |
function logRedirectUri() { | |
Logger.log(getService().getRedirectUri()); | |
} |
Hi, @goelp!
Thank you for the code, it's all working.
But when I'm trying to get some info from the QB it sends me the 403 error.
Basically, I have some problem with authorization, but I still can't find where might be the issue.
I use the code to get - as the @MotorCityCobra wrote:
function getInvoices() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('TEST');
var url = "https://quickbooks.api.intuit.com/v3/company/" + companyID + "/account?minorversion=65"
var headers = {
"muteHttpExceptions": true,
"headers": {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer " + getService().getAccessToken()
}
};
var response = UrlFetchApp.fetch(url, headers);
var data = JSON.parse(response.getContentText());
sheet.appendRow([JSON.stringify(data)]);
}
I've googled the error code - but I see that everything is correct with token, redirect link, scope...
this API blows my mind :-)
I keep getting this.
`Exception: Request failed for https://quickbooks.api.intuit.com returned code 404. Truncated server response:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Nightwielder1 may be its got with the encoding of the URL properly.
Example, when encoded correctly:
This:
https://quickbooks.api.intuit.com/v3/company/1234/query?query=SELECT FROM Customer WHERE Metadata.LastUpdatedTime > '2011-08-10T10:20:30-0700'
Becomes:
https://quickbooks.api.intuit.com/v3/company/1234/query?query=SELECT%20FROM%20Customer%20WHERE%20Metadata.LastUpdatedTime%20%3E+%272011-08-10T10%3A20%3A30-0700%27
You could try using an online encoder like this one: https://meyerweb.com/eric/tools/dencoder/