Skip to content

Instantly share code, notes, and snippets.

@andrewroberts
Last active December 17, 2017 15:27
Show Gist options
  • Save andrewroberts/fed16cc1c7fed9c6d805ffd077efe8c7 to your computer and use it in GitHub Desktop.
Save andrewroberts/fed16cc1c7fed9c6d805ffd077efe8c7 to your computer and use it in GitHub Desktop.
Google Apps Script request to Xero API for trial balance - snippet for Xero Forum question
function getTrialBalancesWithNoDate() {
// .
// .
// .
fetchPublicAppData('Reports/TrialBalance', '', '') // OK
// .
// .
// .
}
function getTrialBalancesWithDate() {
// .
// .
// .
fetchPublicAppData('Reports/TrialBalance', '2016-07-01', 'date') // Error - Not authorised
// .
// .
// .
}
function getInvoices() {
// .
// .
// .
fetchPublicAppData('Invoices', pageNumber, 'page') // OK
// .
// .
// .
}
function fetchPublicAppData (item, parameter, query) {
/* For PUBLIC APPLICATION TYPE */
if (typeof query !== 'undefined' && query !== '') {
query = query + '=' + parameter
} else {
query = ''
}
Log.fine('query: ' + query)
this.loadSettings(); // get latest settings
var method = 'GET'
var requestURL = API_END_POINT + '/' + item
var oauth_signature_method = 'HMAC-SHA1'
var oauth_timestamp = (new Date().getTime()/1000).toFixed()
var oauth_nonce = Utils_.generateRandomString(Math.floor(Math.random() * 50))
var oauth_version = '1.0'
var signBase = 'GET' + '&' + encodeURIComponent(requestURL) + '&'
if ((item === TRIAL_BALANCE_URL) && query !== '') {
signBase += encodeURIComponent(query + '&')
}
signBase += encodeURIComponent(
'oauth_consumer_key=' + this.consumerKey + '&' +
'oauth_nonce=' + oauth_nonce + '&' +
'oauth_signature_method=' + oauth_signature_method + '&' +
'oauth_timestamp=' + oauth_timestamp + '&' +
'oauth_token=' + this.accessToken + '&' +
'oauth_version=' + oauth_version
)
if (item === INVOICE_URL && query !== '') {
signBase += encodeURIComponent('&' + query)
}
// signBase has too many strange chars to use Log.fine(), use Logger
Logger.log(signBase)
// Log.fine('signBase: ' + signBase);
var sbSigned = Utilities
.computeHmacSignature(
Utilities.MacAlgorithm.HMAC_SHA_1,
signBase,
encodeURIComponent(this.consumerSecret) + '&' + encodeURIComponent(this.accessTokenSecret));
Log.fine('sbSigned: ' + sbSigned);
var oauth_signature = Utilities.base64Encode(sbSigned);
Log.fine('oauth_signature: ' + oauth_signature);
var authHeader =
"OAuth oauth_consumer_key=\"" + this.consumerKey +
"\",oauth_nonce=\"" + oauth_nonce +
"\",oauth_token=\"" + this.accessToken +
"\",oauth_signature_method=\"" + oauth_signature_method +
"\",oauth_timestamp=\"" + oauth_timestamp +
"\",oauth_version=\"" + oauth_version +
"\",oauth_signature=\"" +
encodeURIComponent(oauth_signature) + "\"";
var headers = {"User-Agent": + this.userAgent, "Authorization": authHeader, "Accept": "application/json"};
var options = {"headers": headers, "muteHttpExceptions": true};
requestURL = requestURL + (query === '' ? '' : '?') + query
Log.fine('requestURL: ' + requestURL)
var response = UrlFetchApp.fetch(requestURL, options);
var responseCode = response.getResponseCode();
var responseText = response.getContentText();
if (responseCode === 200) {
return JSON.parse(responseText);
} else if (responseCode === 401) {
PropertiesService.getScriptProperties().setProperty('isConnected', 'false')
onOpen() // Reset menu
throw new Error('The Auth token has expired, run Xero > Settings (connect)');
} else {
throw new Error(responseText);
}
} // fetchPublicAppData()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment