Skip to content

Commit

Permalink
quickbooks-cli tool for easy access token and secret getting
Browse files Browse the repository at this point in the history
  • Loading branch information
humrochagf committed May 9, 2017
1 parent a3de2c2 commit 21da147
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
Empty file added quickbooks/tools/__init__.py
Empty file.
73 changes: 73 additions & 0 deletions quickbooks/tools/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import urlparse
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

from quickbooks import QuickBooks


class QuickBooksAuthHandler(BaseHTTPRequestHandler):

consumer_key = ''
consumer_secret = ''
authorize_url = ''
request_token = ''
request_token_secret = ''
sandbox = False

def _set_headers(self):
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()

def do_GET(self):
self._set_headers()

GET = urlparse.parse_qs(urlparse.urlparse(self.path).query)

oauth_verifier = GET.get('oauth_verifier')
realm_id = GET.get('realmId')
if oauth_verifier and realm_id:
client = QuickBooks(
sandbox=self.sandbox,
consumer_key=self.consumer_key,
consumer_secret=self.consumer_secret
)

client.authorize_url = self.authorize_url
client.request_token = self.request_token
client.request_token_secret = self.request_token_secret
client.set_up_service()

client.get_access_tokens(oauth_verifier)

self.wfile.write("<h1>QuickBooks auth handled with success!</h1>")
self.wfile.write('<p><b>Sandbox:</b> {}</p>'.format(self.sandbox))
self.wfile.write('<p><b>Realm Id:</b> {}</p>'.format(realm_id[0]))
self.wfile.write('<p><b>Access Token:</b> {}</p>'.format(
client.access_token))
self.wfile.write('<p><b>Access Token Secret:</b> {}</p>'.format(
client.access_token_secret))
else:
self.wfile.write("<h1>QuickBooks auth failed, try again.</h1>")


def handle_auth(consumer_key, consumer_secret, sandbox=False, port=8080):
client = QuickBooks(
sandbox=sandbox,
consumer_key=consumer_key,
consumer_secret=consumer_secret,
callback_url='http://localhost:{}'.format(port)
)

# ugly hack, needs fixing
QuickBooksAuthHandler.authorize_url = client.get_authorize_url()
QuickBooksAuthHandler.request_token = client.request_token
QuickBooksAuthHandler.request_token_secret = client.request_token_secret
QuickBooksAuthHandler.consumer_key = consumer_key
QuickBooksAuthHandler.consumer_secret = consumer_secret
QuickBooksAuthHandler.sandbox = sandbox

print 'Authorization url (ctrl+click to access):\n{}'.format(
QuickBooksAuthHandler.authorize_url)

server = HTTPServer(('', port), QuickBooksAuthHandler)
server.serve_forever()
34 changes: 34 additions & 0 deletions quickbooks/tools/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import argparse

from auth import handle_auth


class CLI(argparse.ArgumentParser):
info = ({
'prog': 'quickbooks-cli',
'description': 'Starts a local server to help on getting token',
'usage': '%(prog)s key secret'
})

def __init__(self):
super(CLI, self).__init__(**self.info)

self.add_argument('consumer_key', type=str, help='consumer key')
self.add_argument('consumer_secret', type=str, help='consumer secret')
self.add_argument('--sandbox', action='store_true', dest='sandbox',
help='sandbox flag')
self.add_argument('--port', type=int, default=8080, dest='port',
help='auth calback port')

def run(self, args=None):
handle_auth(args.consumer_key, args.consumer_secret,
args.sandbox, args.port)


def cli_execute():
cli = CLI()
cli.run(cli.parse_args())


if __name__ == '__main__':
cli_execute()
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def read(*parts):
keywords=['quickbooks', 'qbo', 'accounting'],
long_description=read('README.rst'),

entry_points={
'console_scripts': ['quickbooks-cli=quickbooks.tools.cli:cli_execute']
},

install_requires=[
'setuptools',
'rauth>=0.7.1',
Expand Down

0 comments on commit 21da147

Please sign in to comment.