Skip to content

Instantly share code, notes, and snippets.

/test.py
Created Nov 2, 2015

Embed
What would you like to do?
#!/usr/bin/python
import time, os, base64, urllib;
# Edit to replace with your own consumer key and secret here (although we're not really using the secret)
CONSUMER_KEY='<your key here>'
CONSUMER_SECRET='<your consumer secret here>'
TIMESTAMP=str(time.time());
ONCE=base64.b64encode(TIMESTAMP);
BASE_URL="https://api.xero.com/api.xro/2.0/organisation"
BASE_URL_ENCODED=urllib.quote_plus(BASE_URL);
# Notice I have taken the care to sort the name=value pairs alphabetically
REQUEST="oauth_consumer_key=" + CONSUMER_KEY + "&oauth_signature_method=RSA-SHA1&oauth_token=" + CONSUMER_KEY
REQUEST_ENCODED=urllib.quote_plus(REQUEST);
SIG_TEXT="GET&" + BASE_URL_ENCODED + "&" + REQUEST_ENCODED
temp = open("text.txt", "wb");
temp.write(SIG_TEXT);
temp.close();
os.system("cat text.txt | openssl dgst -sha1 -sign privatekey.pem -binary > signature.bin")
SIG_BIN = open( "signature.bin", 'r' ).read()
SIGNATURE=base64.b64encode(SIG_BIN);
URL=BASE_URL+ "?" + REQUEST + "&oauth_signature=" + SIGNATURE;
print "Consumer Key: " + CONSUMER_KEY;
print "Unix epoch time: " + TIMESTAMP;
print "Once key: " + ONCE;
print "Base URL: " + BASE_URL;
print "Base URL encoded: " + BASE_URL_ENCODED;
print "Request parameters: " + REQUEST;
print "Request parameters encoded: " + REQUEST_ENCODED;
print "Text that we sign: " + SIG_TEXT;
#print "Signature binary: " + SIG_BIN;
print "Signature: " + SIGNATURE;
print "Complete Request: " + URL
print "Result..."
os.system('curl --pubkey publickey.cer --key privatekey.pem "' + URL + '"');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.