16
16
17
17
import json
18
18
import requests
19
- from future .moves .urllib .parse import urlencode
19
+
20
+ try :
21
+ from urllib .parse import urlencode
22
+ except (ModuleNotFoundError , ImportError ):
23
+ from future .moves .urllib .parse import urlencode
20
24
21
25
from intuitlib .utils import (
22
26
get_discovery_doc ,
@@ -32,7 +36,7 @@ class AuthClient(requests.Session):
32
36
33
37
def __init__ (self , client_id , client_secret , redirect_uri , environment , state_token = None , access_token = None , refresh_token = None , id_token = None , realm_id = None ):
34
38
"""Constructor for AuthClient
35
-
39
+
36
40
:param client_id: Client ID found in developer account Keys tab
37
41
:param client_secret: Client Secret found in developer account Keys tab
38
42
:param redirect_uri: Redirect URI, handles callback from provider
@@ -68,7 +72,7 @@ def __init__(self, client_id, client_secret, redirect_uri, environment, state_to
68
72
self .refresh_token = refresh_token
69
73
self .x_refresh_token_expires_in = None
70
74
self .id_token = id_token
71
-
75
+
72
76
def setAuthorizeURLs (self , urlObject ):
73
77
"""Set authorization url using custom values passed in the data dict
74
78
:param **data: data dict for custom authorizationURLS
@@ -80,17 +84,17 @@ def setAuthorizeURLs(self, urlObject):
80
84
self .revoke_endpoint = urlObject ['revoke_endpoint' ]
81
85
self .user_info_url = urlObject ['user_info_url' ]
82
86
return None
83
-
87
+
84
88
def get_authorization_url (self , scopes , state_token = None ):
85
89
"""Generates authorization url using scopes specified where user is redirected to
86
-
90
+
87
91
:param scopes: Scopes for OAuth/OpenId flow
88
92
:type scopes: list of enum, `intuitlib.enums.Scopes`
89
93
:param state_token: CSRF token, defaults to None
90
94
:return: Authorization url
91
95
"""
92
96
93
- state = state_token or self .state_token
97
+ state = state_token or self .state_token
94
98
if state is None :
95
99
state = generate_token ()
96
100
self .state_token = state
@@ -100,14 +104,14 @@ def get_authorization_url(self, scopes, state_token=None):
100
104
'response_type' : 'code' ,
101
105
'scope' : scopes_to_string (scopes ),
102
106
'redirect_uri' : self .redirect_uri ,
103
- 'state' : self .state_token
107
+ 'state' : self .state_token
104
108
}
105
109
106
110
return '?' .join ([self .auth_endpoint , urlencode (url_params )])
107
111
108
112
def get_bearer_token (self , auth_code , realm_id = None ):
109
113
"""Gets access_token and refresh_token using authorization code
110
-
114
+
111
115
:param auth_code: Authorization code received from redirect_uri
112
116
:param realm_id: Realm ID/Company ID of the QBO company
113
117
:raises `intuitlib.exceptions.AuthClientError`: if response status != 200
@@ -116,7 +120,7 @@ def get_bearer_token(self, auth_code, realm_id=None):
116
120
realm = realm_id or self .realm_id
117
121
if realm is not None :
118
122
self .realm_id = realm
119
-
123
+
120
124
headers = {
121
125
'Content-Type' : 'application/x-www-form-urlencoded' ,
122
126
'Authorization' : get_auth_header (self .client_id , self .client_secret )
@@ -128,11 +132,11 @@ def get_bearer_token(self, auth_code, realm_id=None):
128
132
'redirect_uri' : self .redirect_uri
129
133
}
130
134
131
- send_request ('POST' , self .token_endpoint , headers , self , body = urlencode (body ), session = self )
135
+ send_request ('POST' , self .token_endpoint , headers , self , body = urlencode (body ), session = self )
132
136
133
137
def refresh (self , refresh_token = None ):
134
- """Gets fresh access_token and refresh_token
135
-
138
+ """Gets fresh access_token and refresh_token
139
+
136
140
:param refresh_token: Refresh Token
137
141
:raises ValueError: if Refresh Token value not specified
138
142
:raises `intuitlib.exceptions.AuthClientError`: if response status != 200
@@ -176,12 +180,12 @@ def revoke(self, token=None):
176
180
'token' : token_to_revoke
177
181
}
178
182
179
- send_request ('POST' , self .revoke_endpoint , headers , self , body = json .dumps (body ), session = self )
183
+ send_request ('POST' , self .revoke_endpoint , headers , self , body = json .dumps (body ), session = self )
180
184
return True
181
-
185
+
182
186
def get_user_info (self , access_token = None ):
183
187
"""Gets User Info based on OpenID scopes specified
184
-
188
+
185
189
:param access_token: Access token
186
190
:raises ValueError: if Refresh Token or Access Token value not specified
187
191
:raises `intuitlib.exceptions.AuthClientError`: if response status != 200
0 commit comments