1616
1717import json
1818import 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
2024
2125from intuitlib .utils import (
2226 get_discovery_doc ,
@@ -32,7 +36,7 @@ class AuthClient(requests.Session):
3236
3337 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 ):
3438 """Constructor for AuthClient
35-
39+
3640 :param client_id: Client ID found in developer account Keys tab
3741 :param client_secret: Client Secret found in developer account Keys tab
3842 :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
6872 self .refresh_token = refresh_token
6973 self .x_refresh_token_expires_in = None
7074 self .id_token = id_token
71-
75+
7276 def setAuthorizeURLs (self , urlObject ):
7377 """Set authorization url using custom values passed in the data dict
7478 :param **data: data dict for custom authorizationURLS
@@ -80,17 +84,17 @@ def setAuthorizeURLs(self, urlObject):
8084 self .revoke_endpoint = urlObject ['revoke_endpoint' ]
8185 self .user_info_url = urlObject ['user_info_url' ]
8286 return None
83-
87+
8488 def get_authorization_url (self , scopes , state_token = None ):
8589 """Generates authorization url using scopes specified where user is redirected to
86-
90+
8791 :param scopes: Scopes for OAuth/OpenId flow
8892 :type scopes: list of enum, `intuitlib.enums.Scopes`
8993 :param state_token: CSRF token, defaults to None
9094 :return: Authorization url
9195 """
9296
93- state = state_token or self .state_token
97+ state = state_token or self .state_token
9498 if state is None :
9599 state = generate_token ()
96100 self .state_token = state
@@ -100,14 +104,14 @@ def get_authorization_url(self, scopes, state_token=None):
100104 'response_type' : 'code' ,
101105 'scope' : scopes_to_string (scopes ),
102106 'redirect_uri' : self .redirect_uri ,
103- 'state' : self .state_token
107+ 'state' : self .state_token
104108 }
105109
106110 return '?' .join ([self .auth_endpoint , urlencode (url_params )])
107111
108112 def get_bearer_token (self , auth_code , realm_id = None ):
109113 """Gets access_token and refresh_token using authorization code
110-
114+
111115 :param auth_code: Authorization code received from redirect_uri
112116 :param realm_id: Realm ID/Company ID of the QBO company
113117 :raises `intuitlib.exceptions.AuthClientError`: if response status != 200
@@ -116,7 +120,7 @@ def get_bearer_token(self, auth_code, realm_id=None):
116120 realm = realm_id or self .realm_id
117121 if realm is not None :
118122 self .realm_id = realm
119-
123+
120124 headers = {
121125 'Content-Type' : 'application/x-www-form-urlencoded' ,
122126 'Authorization' : get_auth_header (self .client_id , self .client_secret )
@@ -128,11 +132,11 @@ def get_bearer_token(self, auth_code, realm_id=None):
128132 'redirect_uri' : self .redirect_uri
129133 }
130134
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 )
132136
133137 def refresh (self , refresh_token = None ):
134- """Gets fresh access_token and refresh_token
135-
138+ """Gets fresh access_token and refresh_token
139+
136140 :param refresh_token: Refresh Token
137141 :raises ValueError: if Refresh Token value not specified
138142 :raises `intuitlib.exceptions.AuthClientError`: if response status != 200
@@ -176,12 +180,12 @@ def revoke(self, token=None):
176180 'token' : token_to_revoke
177181 }
178182
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 )
180184 return True
181-
185+
182186 def get_user_info (self , access_token = None ):
183187 """Gets User Info based on OpenID scopes specified
184-
188+
185189 :param access_token: Access token
186190 :raises ValueError: if Refresh Token or Access Token value not specified
187191 :raises `intuitlib.exceptions.AuthClientError`: if response status != 200
0 commit comments