@@ -54,8 +54,7 @@ def exchange_user_id_for_access_token(self, user_id):
5454 def exchange_xauth_login_for_access_token (self , username , password , scope = None ):
5555 """ scope should be a tuple or list of requested scope access levels """
5656 req = OAuth2AuthExchangeRequest (self )
57- return req .exchange_for_access_token (username = username , password = password ,
58- scope = scope )
57+ return req .exchange_for_access_token (username = username , password = password , scope = scope )
5958
6059
6160class OAuth2AuthExchangeRequest (object ):
@@ -68,8 +67,10 @@ def _url_for_authorize(self, scope=None):
6867 "response_type" : "code" ,
6968 "redirect_uri" : self .api .redirect_uri
7069 }
70+
7171 if scope :
7272 client_params .update (scope = ' ' .join (scope ))
73+
7374 url_params = urlencode (client_params )
7475 return "%s?%s" % (self .api .authorize_url , url_params )
7576
@@ -80,6 +81,7 @@ def _data_for_exchange(self, code=None, username=None, password=None, scope=None
8081 "redirect_uri" : self .api .redirect_uri ,
8182 "grant_type" : "authorization_code"
8283 }
84+
8385 if code :
8486 client_params .update (code = code )
8587 elif username and password :
@@ -88,8 +90,10 @@ def _data_for_exchange(self, code=None, username=None, password=None, scope=None
8890 grant_type = "password" )
8991 if scope :
9092 client_params .update (scope = ' ' .join (scope ))
93+
9194 elif user_id :
9295 client_params .update (user_id = user_id )
96+
9397 return urlencode (client_params )
9498
9599 def get_authorize_url (self , scope = None ):
@@ -102,18 +106,22 @@ def get_authorize_login_url(self, scope=None):
102106 response , content = http_object .request (url )
103107 if response ['status' ] != '200' :
104108 raise OAuth2AuthExchangeError ("The server returned a non-200 response for URL %s" % url )
105- redirected_to = response ['content-location' ]
109+
110+ redirected_to = response ['Content-Location' ]
106111 return redirected_to
107112
108113 def exchange_for_access_token (self , code = None , username = None , password = None , scope = None , user_id = None ):
109114 data = self ._data_for_exchange (code , username , password , scope = scope , user_id = user_id )
110115 http_object = Http (disable_ssl_certificate_validation = True )
111116 url = self .api .access_token_url
112- headers = {"Content-Type" :"application/x-www-form-urlencoded" }
117+
118+ headers = {"Content-Type" : "application/x-www-form-urlencoded" }
113119 response , content = http_object .request (url , method = "POST" , body = data , headers = headers )
114120 parsed_content = simplejson .loads (content .decode ())
121+
115122 if int (response ['status' ]) != 200 :
116123 raise OAuth2AuthExchangeError (parsed_content .get ("error_message" , "" ))
124+
117125 return parsed_content ['access_token' ], parsed_content ['user' ]
118126
119127
@@ -137,15 +145,12 @@ def post_request(self, path, **kwargs):
137145 return self .make_request (self .prepare_request ("POST" , path , kwargs ))
138146
139147 def _full_url (self , path , include_secret = False , include_signed_request = True ):
140- return "%s://%s%s%s%s%s" % (self .api .protocol ,
141- self .api .host ,
142- self .api .base_path ,
143- path ,
144- self ._auth_query (include_secret ),
145- self ._signed_request (path , {}, include_signed_request , include_secret ))
148+ return "%s://%s%s%s%s%s" % (self .api .protocol , self .api .host , self .api .base_path , path ,
149+ self ._auth_query (include_secret ),
150+ self ._signed_request (path , {}, include_signed_request , include_secret ))
146151
147152 def _full_url_with_params (self , path , params , include_secret = False , include_signed_request = True ):
148- return (self ._full_url (path , include_secret ) +
153+ return (self ._full_url (path , include_secret ) +
149154 self ._full_query_with_params (params ) +
150155 self ._signed_request (path , params , include_signed_request , include_secret ))
151156
@@ -168,8 +173,10 @@ def _signed_request(self, path, params, include_signed_request, include_secret):
168173 params ['access_token' ] = self .api .access_token
169174 elif self .api .client_id :
170175 params ['client_id' ] = self .api .client_id
176+
171177 if include_secret and self .api .client_secret :
172178 params ['client_secret' ] = self .api .client_secret
179+
173180 return "&sig=%s" % self ._generate_sig (path , params , self .api .client_secret )
174181 else :
175182 return ''
@@ -200,6 +207,7 @@ def encode_file(field_name):
200207 lines .extend (encode_field (field ))
201208 for field in files :
202209 lines .extend (encode_file (field ))
210+
203211 lines .extend (("--%s--" % (boundary ), "" ))
204212 body = "\r \n " .join (lines )
205213
@@ -219,7 +227,7 @@ def prepare_request(self, method, path, params, include_secret=False):
219227 if not params .get ('files' ):
220228 if method == "POST" :
221229 body = self ._post_body (params )
222- headers = {'Content-type ' : 'application/x-www-form-urlencoded' }
230+ headers = {'Content-Type ' : 'application/x-www-form-urlencoded' }
223231 url = self ._full_url (path , include_secret )
224232 else :
225233 url = self ._full_url_with_params (path , params , include_secret )
@@ -231,9 +239,11 @@ def prepare_request(self, method, path, params, include_secret=False):
231239
232240 def make_request (self , url , method = "GET" , body = None , headers = None ):
233241 headers = headers or {}
234- if not 'User-Agent' in headers :
242+ if 'User-Agent' not in headers :
235243 headers .update ({"User-Agent" : "%s Python Client" % self .api .api_name })
244+
236245 # https://github.com/jcgregorio/httplib2/issues/173
237246 # bug in httplib2 w/ Python 3 and disable_ssl_certificate_validation=True
238- http_obj = Http () if six .PY3 else Http (disable_ssl_certificate_validation = True )
247+ http_obj = Http () if six .PY3 else Http (disable_ssl_certificate_validation = True )
248+
239249 return http_obj .request (url , method , body = body , headers = headers )
0 commit comments