forked from ej2/python-quickbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quickbooks-cli tool for easy access token and secret getting
- Loading branch information
1 parent
a3de2c2
commit 21da147
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters