Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Updated code from here(https://help.interfaceware.com/kb/writing-a-xero-adapter) to work with python 3.x
- // Requires openssl and curl in the environment path variable
- // Run with python [filename].py
- #!/usr/bin/python
- import time, os, base64, urllib.parse as urllib, random, collections, binascii;
- def OutputTable(Name, Table):
- print(Name);
- for key, value in Table.items():
- print(" ", key, ": ", value);
- def OauthEscape(Value):
- Result = urllib.quote_plus(Value);
- Result = Result.replace("+", "%20"); # cheap hack
- return Result;
- def ConcatenateSigParams(Params):
- X = "";
- for K,V in Params.items():
- X = X+K+'='+OauthEscape(V)+'&'
- return X[:-1]; # remove the last character
- def AuthInfo(Auth):
- R = "";
- for K,V in Auth.items():
- R = R+' '+K+'="'+V+'",'
- return R
- CONSUMER_KEY='INSERT CONSUMER KEY HERE'
- Url = 'https://api.xero.com/api.xro/2.0/ENDPOINT/GUID/history'
- Params = {}
- #Params['where'] = 'Name == "Kevin Macey"' # uncomment and remove GUID/history and add contacts as endpoint to search contacts
- Headers = {}
- Auth = {}
- Auth['oauth_nonce'] = str(int(time.time())) + str(int(random.random()*1000000));
- Auth['oauth_timestamp'] = str(int(time.time()));
- Auth['oauth_version'] = '1.0';
- Auth['oauth_signature_method'] = 'RSA-SHA1';
- Auth['oauth_consumer_key'] = CONSUMER_KEY;
- Auth['oauth_token'] = CONSUMER_KEY;
- # Merge GET params with oauth params
- AllParams = {}
- for key, value in Params.items():
- AllParams[key] = value;
- for key, value in Auth.items():
- AllParams[key] = value;
- OrderedParams = collections.OrderedDict(sorted(AllParams.items()));
- SortedParamAuthString = ConcatenateSigParams(OrderedParams);
- SignatureText = 'GET&'+OauthEscape(Url)+"&"+OauthEscape(SortedParamAuthString);
- temp = open("text.txt", "wb");
- temp.write(SignatureText.encode('utf-8'));
- temp.close();
- os.system("cat text.txt | openssl dgst -sha1 -sign privatekey.pem -binary > signature.bin")
- SignatureBinary = open( "signature.bin", 'rb' ).read();
- Signature=base64.b64encode(SignatureBinary);
- Headers['Authorization'] = 'OAuth' + AuthInfo(Auth) + ' oauth_signature="' + OauthEscape(Signature) + '"'
- Headers['Accept'] = "application/json"
- print("==========================================================================")
- OutputTable('Params', Params);
- print("==========================================================================")
- OutputTable('Oauth Headers', Auth);
- print("==========================================================================")
- OutputTable('All Parameters', AllParams);
- print("==========================================================================")
- OutputTable('Ordered Parameters', OrderedParams);
- print("==========================================================================")
- OutputTable('Headers', Headers);
- print("==========================================================================")
- print("Sorted Parmam String: ", SortedParamAuthString);
- print("==========================================================================")
- print("Signature Text: ", SignatureText);
- print("==========================================================================")
- print("Signature : ", Signature);
- print("==========================================================================")
- CurlCommand = "curl -vv"
- # We have to escape the " characters to \" for the windows command line
- for key, value in Headers.items():
- CurlCommand += ' --header \'' + key + ": " + value.replace('"', '\"') + "\'"
- CurlCommand += " \"" + Url + "\"?";
- # Add the GET variables
- for key, value in Params.items():
- CurlCommand += key + "=" + urllib.quote_plus(value) + "&";
- # Strip off the last & character
- CurlCommand = CurlCommand[:-1]
- print(CurlCommand);
- os.system(CurlCommand);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement