Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 861d2fd

Browse files
authored
Merge pull request ej2#74 from humrochagf/master
QuickBooks command line tool
2 parents 02d17ac + 3a0c029 commit 861d2fd

File tree

7 files changed

+179
-0
lines changed

7 files changed

+179
-0
lines changed

README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ You can find additional examples of usage in `Integration tests folder`_.
1414
Connecting your application to Quickbooks Online
1515
------------------------------------------------
1616

17+
Theres two ways to connect your application to Quickbooks Online:
18+
19+
With quickbooks-cli
20+
-------------------
21+
22+
::
23+
quickbooks-cli [-h] [-s] [-p PORT] consumer_key consumer_secret
24+
25+
Manually
26+
--------
27+
1728
1. Create the Authorization URL for your application:
1829

1930
::

quickbooks/tools/__init__.py

Whitespace-only changes.

quickbooks/tools/auth.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
try: # Python 3
2+
from urllib.parse import parse_qs, urlparse
3+
from http.server import BaseHTTPRequestHandler, HTTPServer
4+
except ImportError: # Python 2
5+
from urlparse import parse_qs, urlparse
6+
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
7+
8+
def bytes(value, encoding):
9+
return str(value)
10+
11+
from quickbooks import QuickBooks
12+
13+
14+
class QuickBooksAuthHandler(BaseHTTPRequestHandler):
15+
16+
def _set_headers(self):
17+
self.send_response(200)
18+
self.send_header('Content-Type', 'text/html')
19+
self.end_headers()
20+
21+
def do_GET(self):
22+
self._set_headers()
23+
qb_data = self.server.qb_data
24+
25+
GET = parse_qs(urlparse(self.path).query)
26+
27+
oauth_verifier = GET.get('oauth_verifier')
28+
realm_id = GET.get('realmId')
29+
30+
if type(realm_id) is list and len(realm_id) == 1:
31+
realm_id = realm_id[0]
32+
33+
if oauth_verifier and realm_id:
34+
client = self.server.qb_client_class(
35+
sandbox=qb_data['sandbox'],
36+
consumer_key=qb_data['consumer_key'],
37+
consumer_secret=qb_data['consumer_secret']
38+
)
39+
40+
client.authorize_url = qb_data['authorize_url']
41+
client.request_token = qb_data['request_token']
42+
client.request_token_secret = qb_data['request_token_secret']
43+
client.set_up_service()
44+
45+
client.get_access_tokens(oauth_verifier)
46+
47+
self.wfile.write(
48+
bytes('<h1>QuickBooks auth handled with success!</h1>',
49+
'UTF-8'))
50+
self.wfile.write(
51+
bytes('<p><b>Sandbox:</b> {0}</p>'.format(qb_data['sandbox']),
52+
'UTF-8'))
53+
self.wfile.write(
54+
bytes('<p><b>Realm Id:</b> {0}</p>'.format(realm_id), 'UTF-8'))
55+
self.wfile.write(
56+
bytes('<p><b>Access Token:</b> {0}</p>'.format(
57+
client.access_token), 'UTF-8'))
58+
self.wfile.write(
59+
bytes('<p><b>Access Token Secret:</b> {0}</p>'.format(
60+
client.access_token_secret), 'UTF-8'))
61+
else:
62+
self.wfile.write(
63+
bytes('<h1>QuickBooks auth failed, try again.</h1>', 'UTF-8'))
64+
65+
66+
class QuickBooksAuthServer(HTTPServer):
67+
68+
qb_client_class = QuickBooks
69+
70+
@classmethod
71+
def build_server(cls, consumer_key, consumer_secret, sandbox, port):
72+
client = cls.qb_client_class(
73+
sandbox=sandbox,
74+
consumer_key=consumer_key,
75+
consumer_secret=consumer_secret,
76+
callback_url='http://localhost:{0}'.format(port)
77+
)
78+
79+
qb_data = {
80+
'authorize_url': client.get_authorize_url(),
81+
'request_token': client.request_token,
82+
'request_token_secret': client.request_token_secret,
83+
'consumer_key': consumer_key,
84+
'consumer_secret': consumer_secret,
85+
'sandbox': sandbox,
86+
}
87+
88+
instance = cls(('', port), QuickBooksAuthHandler)
89+
instance.qb_data = qb_data
90+
91+
return instance

quickbooks/tools/cli.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import argparse
2+
3+
from quickbooks.tools.auth import QuickBooksAuthServer
4+
5+
6+
class CLI(argparse.ArgumentParser):
7+
info = ({
8+
'prog': 'quickbooks-cli',
9+
'description': 'Starts a local server to handle quickbooks auth',
10+
})
11+
12+
def __init__(self):
13+
super(CLI, self).__init__(**self.info)
14+
15+
self.add_argument('consumer_key', type=str,
16+
help='quickbooks consumer key')
17+
self.add_argument('consumer_secret', type=str,
18+
help='quickbooks consumer secret')
19+
self.add_argument('-s', '-sandbox', action='store_true',
20+
dest='sandbox', help='sandbox flag')
21+
self.add_argument('-p', '--port', type=int, default=8080,
22+
dest='port', help='auth calback port')
23+
24+
def run(self, args=None):
25+
print('Starting the authentication process...')
26+
27+
server = QuickBooksAuthServer.build_server(
28+
args.consumer_key, args.consumer_secret, args.sandbox, args.port)
29+
30+
print('Copy and paste the authorization url on your browser:')
31+
print(server.qb_data['authorize_url'])
32+
33+
server.serve_forever()
34+
35+
36+
def cli_execute():
37+
cli = CLI()
38+
cli.run(cli.parse_args())
39+
40+
41+
if __name__ == '__main__':
42+
cli_execute()

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def read(*parts):
2424
keywords=['quickbooks', 'qbo', 'accounting'],
2525
long_description=read('README.rst'),
2626

27+
entry_points={
28+
'console_scripts': ['quickbooks-cli=quickbooks.tools.cli:cli_execute']
29+
},
30+
2731
install_requires=[
2832
'setuptools',
2933
'rauth>=0.7.1',

tests/unit/tools/__init__.py

Whitespace-only changes.

tests/unit/tools/test_auth.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unittest
2+
3+
try:
4+
from mock import patch
5+
except ImportError:
6+
from unittest.mock import patch
7+
8+
from quickbooks.tools.auth import QuickBooksAuthServer
9+
10+
11+
class ClientTest(unittest.TestCase):
12+
13+
def setUp(self):
14+
self.consumer_key = 'update_consumer_key'
15+
self.consumer_secret = 'update_consumer_secret'
16+
self.sandbox = True
17+
self.port = 8080
18+
self.authorize_url = '{0}?oauth_token=testToken'.format(
19+
QuickBooksAuthServer.qb_client_class.authorize_url)
20+
21+
def test_build_server(self):
22+
with patch.object(QuickBooksAuthServer.qb_client_class,
23+
'get_authorize_url',
24+
return_value=self.authorize_url):
25+
server = QuickBooksAuthServer.build_server(
26+
self.consumer_key, self.consumer_secret,
27+
self.sandbox, self.port)
28+
29+
self.assertTrue(isinstance(server, QuickBooksAuthServer))
30+
self.assertTrue(isinstance(server.qb_data, dict))
31+
self.assertTrue('oauth_token' in server.qb_data['authorize_url'])

0 commit comments

Comments
 (0)