Skip to content

Commit 5029157

Browse files
authored
Merge pull request #358 from greyli/patch-1
Add support for string type token
2 parents ed06006 + c62bd9d commit 5029157

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

flask_oauthlib/client.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
log = logging.getLogger('flask_oauthlib')
2828

2929

30+
if PY3:
31+
string_types = (str,)
32+
else:
33+
string_types = (str, unicode)
34+
35+
3036
__all__ = ('OAuth', 'OAuthRemoteApp', 'OAuthResponse', 'OAuthException')
3137

3238

@@ -375,8 +381,11 @@ def make_client(self, token=None):
375381
**params
376382
)
377383
else:
378-
if token and isinstance(token, (tuple, list)):
379-
token = {'access_token': token[0]}
384+
if token:
385+
if isinstance(token, (tuple, list)):
386+
token = {'access_token': token[0]}
387+
elif isinstance(token, string_types):
388+
token = {'access_token': token}
380389
client = oauthlib.oauth2.WebApplicationClient(
381390
self.consumer_key, token=token
382391
)

tests/test_client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from flask_oauthlib.client import encode_request_data
44
from flask_oauthlib.client import OAuthRemoteApp, OAuth
55
from flask_oauthlib.client import parse_response
6+
from oauthlib.common import PY3
67

78
try:
89
import urllib2 as http
@@ -184,3 +185,32 @@ class _Faker(object):
184185
resp, content = OAuthRemoteApp.http_request('http://example.com')
185186
assert resp.code == 404
186187
assert b'o' in content
188+
189+
def test_token_types(self):
190+
oauth = OAuth()
191+
remote = oauth.remote_app('remote',
192+
consumer_key='remote key',
193+
consumer_secret='remote secret')
194+
195+
client_token = {'access_token': 'access token'}
196+
197+
if not PY3:
198+
unicode_token = u'access token'
199+
client = remote.make_client(token=unicode_token)
200+
assert client.token == client_token
201+
202+
str_token = 'access token'
203+
client = remote.make_client(token=str_token)
204+
assert client.token == client_token
205+
206+
list_token = ['access token']
207+
client = remote.make_client(token=list_token)
208+
assert client.token == client_token
209+
210+
tuple_token = ('access token',)
211+
client = remote.make_client(token=tuple_token)
212+
assert client.token == client_token
213+
214+
dict_token = {'access_token': 'access token'}
215+
client = remote.make_client(token=dict_token)
216+
assert client.token == client_token

0 commit comments

Comments
 (0)