forked from routablehq/python-quickbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
161 lines (133 loc) · 5.79 KB
/
Copy pathauth.py
File metadata and controls
161 lines (133 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from quickbooks.auth import Oauth1SessionManager, Oauth2SessionManager
try: # Python 3
from urllib.parse import parse_qs, urlparse
from http.server import BaseHTTPRequestHandler, HTTPServer
except ImportError: # Python 2
from urlparse import parse_qs, urlparse
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
def bytes(value, encoding):
return str(value)
class QuickBooksAuthHandler(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
def do_GET(self):
qb_data = self.server.qb_data
oauth_version = qb_data['oauth_version']
if oauth_version == 1:
self.get_oauth_1()
elif oauth_version == 2:
self.get_oauth_2()
def get_oauth_1(self):
self._set_headers()
qb_data = self.server.qb_data
GET = parse_qs(urlparse(self.path).query)
oauth_verifier = GET.get('oauth_verifier')
realm_id = GET.get('realmId')
if type(realm_id) is list and len(realm_id) == 1:
realm_id = realm_id[0]
if oauth_verifier and realm_id:
client = Oauth1SessionManager(
sandbox=qb_data['sandbox'],
consumer_key=qb_data['consumer_key'],
consumer_secret=qb_data['consumer_secret']
)
client.authorize_url = qb_data['authorize_url']
client.request_token = qb_data['request_token']
client.request_token_secret = qb_data['request_token_secret']
client.get_access_tokens(oauth_verifier)
self.wfile.write(
bytes('<h1>QuickBooks auth handled with success!</h1>',
'UTF-8'))
self.wfile.write(
bytes('<p><b>Sandbox:</b> {0}</p>'.format(qb_data['sandbox']),
'UTF-8'))
self.wfile.write(
bytes('<p><b>Realm Id:</b> {0}</p>'.format(realm_id), 'UTF-8'))
self.wfile.write(
bytes('<p><b>Access Token:</b> {0}</p>'.format(
client.access_token), 'UTF-8'))
self.wfile.write(
bytes('<p><b>Access Token Secret:</b> {0}</p>'.format(
client.access_token_secret), 'UTF-8'))
else:
self.wfile.write(
bytes('<h1>QuickBooks auth failed, try again.</h1>', 'UTF-8'))
def get_oauth_2(self):
self._set_headers()
qb_data = self.server.qb_data
GET = parse_qs(urlparse(self.path).query)
auth_code = GET.get('code')
if auth_code:
client = Oauth2SessionManager(
sandbox=qb_data['sandbox'],
client_id=qb_data['consumer_key'],
client_secret=qb_data['consumer_secret'],
callback_url=qb_data['callback_url'],
base_url=qb_data['base_url'],
)
client.get_access_tokens(auth_code)
self.wfile.write(
bytes('<h1>QuickBooks auth handled with success!</h1>',
'UTF-8'))
self.wfile.write(
bytes('<p><b>Sandbox:</b> {0}</p>'.format(qb_data['sandbox']),
'UTF-8'))
self.wfile.write(
bytes('<p><b>Access Token:</b> {0}</p>'.format(
client.access_token), 'UTF-8'))
self.wfile.write(
bytes('<p><b>Refresh Token:</b> {0}</p>'.format(
client.refresh_token), 'UTF-8'))
self.wfile.write(
bytes('<p><b>Expires In:</b> {0}</p>'.format(
client.expires_in), 'UTF-8'))
else:
self.wfile.write(
bytes('<h1>QuickBooks auth failed, try again.</h1>', 'UTF-8'))
class QuickBooksAuthServer(HTTPServer):
@classmethod
def build_server(cls, consumer_key, consumer_secret, sandbox, port, oauth_version):
callback_url = 'http://localhost:{0}'.format(port)
if oauth_version == 1:
client = Oauth1SessionManager(
sandbox=sandbox,
consumer_key=consumer_key,
consumer_secret=consumer_secret,
)
qb_data = {
'authorize_url': client.get_authorize_url(callback_url),
'request_token': client.request_token,
'request_token_secret': client.request_token_secret,
'consumer_key': consumer_key,
'consumer_secret': consumer_secret,
'sandbox': sandbox,
'oauth_version': oauth_version,
}
elif oauth_version == 2:
client = Oauth2SessionManager(
sandbox=sandbox,
client_id=consumer_key,
client_secret=consumer_secret,
base_url='http://localhost:{0}'.format(port),
)
qb_data = {
'authorize_url': client.get_authorize_url(callback_url),
'access_token': client.access_token,
'refresh_token': client.refresh_token,
'consumer_key': consumer_key,
'consumer_secret': consumer_secret,
'token_type': client.token_type,
'expires_in': client.expires_in,
'x_refresh_token_expires_in': client.x_refresh_token_expires_in,
'sandbox': sandbox,
'oauth_version': oauth_version,
'callback_url': 'http://localhost:{0}'.format(port),
'base_url': 'http://localhost:{0}'.format(port),
}
else:
raise Exception('Invalid OAuth version number. Version number must be 1 or 2.')
instance = cls(('', port), QuickBooksAuthHandler)
instance.qb_data = qb_data
return instance