Skip to content

Instantly share code, notes, and snippets.

@john2x
Created October 11, 2012 11:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save john2x/3871758 to your computer and use it in GitHub Desktop.
Save john2x/3871758 to your computer and use it in GitHub Desktop.
xero 2-legged oauth
function test() {
var requestURL = "https://api.xero.com/api.xro/2.0/Accounts",
timestamp = (new Date().valueOf()/1000).toFixed(0),
nonce = createGuid(),
key = "consumer key from xero private app",
publickey = "content of publickey.cer",
requestData = {
"method": "GET",
"contentType": "application/xml",
"headers": {
"AUTHORIZATION": "OAuth realm=\"https://api.xero.com/\"," +
"oauth_consumer_key=\"" + key + "\"," +
"oauth_token=\"" + key + "\"," +
"oauth_signature_method=\"" + "RSA-SHA1" + "\"," +
"oauth_signature=\"" + publickey + "\"," +
"oauth_timestamp=\"" + timestamp + "\"," +
"oauth_nonce=\"" + nonce + "\"," +
"oauth_version=\"1.0\"",
"User-Agent": "my-private-app"
}
};
var result = UrlFetchApp.fetch(requestURL, requestData);
Logger.log(result.getContentText());
}
function createGuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment