Advertisement
image28

Xero Python API Access

Dec 11th, 2018
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.81 KB | None | 0 0
  1. // Updated code from here(https://help.interfaceware.com/kb/writing-a-xero-adapter) to work with python 3.x
  2. // Requires openssl and curl in the environment path variable
  3. // Run with python [filename].py
  4.  
  5. #!/usr/bin/python
  6. import time, os, base64, urllib.parse as urllib, random, collections, binascii;
  7.  
  8. def OutputTable(Name, Table):
  9.    print(Name);
  10.    for key, value in Table.items():
  11.       print(" ", key, ": ", value);
  12.  
  13. def OauthEscape(Value):
  14.    Result = urllib.quote_plus(Value);
  15.    Result = Result.replace("+", "%20"); # cheap hack
  16.    return Result;
  17.  
  18. def ConcatenateSigParams(Params):
  19.    X = "";
  20.    for K,V in Params.items():
  21.       X = X+K+'='+OauthEscape(V)+'&'
  22.    return X[:-1]; # remove the last character
  23.  
  24. def AuthInfo(Auth):
  25.    R = "";
  26.    for K,V in Auth.items():
  27.     R = R+' '+K+'="'+V+'",'
  28.    return R
  29.  
  30. CONSUMER_KEY='INSERT CONSUMER KEY HERE'
  31. Url = 'https://api.xero.com/api.xro/2.0/ENDPOINT/GUID/history'
  32. Params = {}
  33. #Params['where'] = 'Name == "Kevin Macey"' # uncomment and remove GUID/history and add contacts as endpoint to search contacts
  34.  
  35. Headers = {}
  36.  
  37. Auth = {}
  38. Auth['oauth_nonce'] = str(int(time.time())) + str(int(random.random()*1000000));
  39. Auth['oauth_timestamp'] = str(int(time.time()));
  40. Auth['oauth_version'] = '1.0';
  41. Auth['oauth_signature_method'] = 'RSA-SHA1';
  42. Auth['oauth_consumer_key'] = CONSUMER_KEY;
  43. Auth['oauth_token'] = CONSUMER_KEY;
  44.  
  45. # Merge GET params with oauth params
  46. AllParams = {}
  47. for key, value in Params.items():
  48.    AllParams[key] = value;
  49. for key, value in Auth.items():
  50.    AllParams[key] = value;
  51.  
  52. OrderedParams = collections.OrderedDict(sorted(AllParams.items()));
  53. SortedParamAuthString = ConcatenateSigParams(OrderedParams);
  54. SignatureText = 'GET&'+OauthEscape(Url)+"&"+OauthEscape(SortedParamAuthString);
  55.  
  56. temp = open("text.txt", "wb");
  57. temp.write(SignatureText.encode('utf-8'));
  58. temp.close();
  59. os.system("cat text.txt | openssl dgst -sha1 -sign privatekey.pem -binary > signature.bin")
  60. SignatureBinary = open( "signature.bin", 'rb' ).read();
  61. Signature=base64.b64encode(SignatureBinary);
  62.  
  63. Headers['Authorization'] = 'OAuth' + AuthInfo(Auth) + ' oauth_signature="' + OauthEscape(Signature) + '"'
  64. Headers['Accept'] = "application/json"
  65. print("==========================================================================")
  66. OutputTable('Params', Params);
  67. print("==========================================================================")
  68. OutputTable('Oauth Headers', Auth);
  69. print("==========================================================================")
  70. OutputTable('All Parameters', AllParams);
  71. print("==========================================================================")
  72. OutputTable('Ordered Parameters', OrderedParams);
  73. print("==========================================================================")
  74. OutputTable('Headers', Headers);
  75. print("==========================================================================")
  76. print("Sorted Parmam String: ", SortedParamAuthString);
  77. print("==========================================================================")
  78. print("Signature Text: ", SignatureText);
  79. print("==========================================================================")
  80. print("Signature : ", Signature);
  81. print("==========================================================================")
  82. CurlCommand = "curl -vv"
  83.  
  84. # We have to escape the " characters to \" for the windows command line
  85. for key, value in Headers.items():
  86.    CurlCommand += ' --header \'' + key + ": " + value.replace('"', '\"') + "\'"
  87.  
  88. CurlCommand += " \"" + Url + "\"?";
  89. # Add the GET variables
  90. for key, value in Params.items():
  91.    CurlCommand += key + "=" + urllib.quote_plus(value) + "&";
  92. # Strip off the last & character
  93. CurlCommand = CurlCommand[:-1]
  94.  
  95. print(CurlCommand);
  96. os.system(CurlCommand);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement