Skip to content

Commit 4f53f36

Browse files
committed
Merge pull request lepture#51 from tonyseek/feature/accept-plain-text-config
Add support for plain text config item
2 parents 0b5a20a + 36771a9 commit 4f53f36

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

docs/client.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ Flask config by the key ``TWITTER``, the configuration looks like::
6565

6666
oauth.init_app(app)
6767

68+
Or looks like that::
69+
70+
app.config['TWITTER_CONSUMER_KEY'] = 'a random string key'
71+
app.config['TWITTER_CONSUMER_SECRET'] = 'a random string secret'
72+
6873
Twitter can get consumer key and secret from the Flask instance now.
6974

7075
You can put all the configuration in ``app.config`` if you like, which

flask_oauthlib/client.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,18 @@ def _get_property(self, key, default=False):
297297
return default
298298
return attr
299299
app = self.oauth.app or current_app
300-
config = app.config[self.app_key]
301-
if default is not False:
302-
return config.get(key, default)
303-
return config[key]
300+
if self.app_key in app.config:
301+
# works with dict config
302+
config = app.config[self.app_key]
303+
if default is not False:
304+
return config.get(key, default)
305+
return config[key]
306+
else:
307+
# works with plain text config
308+
config_key = "%s_%s" % (self.app_key, key.upper())
309+
if default is not False:
310+
return app.config.get(config_key, default)
311+
return app.config[config_key]
304312

305313
def make_client(self, token=None):
306314
# request_token_url is for oauth1

tests/test_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ def test_lazy_load(self):
114114
assert twitter.authorize_url == 'auth url'
115115
assert twitter.content_type is None
116116

117+
def test_lazy_load_with_plain_text_config(self):
118+
oauth = OAuth()
119+
twitter = oauth.remote_app('twitter', app_key='TWITTER')
120+
121+
app = Flask(__name__)
122+
app.config['TWITTER_CONSUMER_KEY'] = 'twitter key'
123+
app.config['TWITTER_CONSUMER_SECRET'] = 'twitter secret'
124+
app.config['TWITTER_REQUEST_TOKEN_URL'] = 'request url'
125+
app.config['TWITTER_ACCESS_TOKEN_URL'] = 'token url'
126+
app.config['TWITTER_AUTHORIZE_URL'] = 'auth url'
127+
128+
oauth.init_app(app)
129+
130+
assert twitter.consumer_key == 'twitter key'
131+
assert twitter.consumer_secret == 'twitter secret'
132+
assert twitter.request_token_url == 'request url'
133+
assert twitter.access_token_url == 'token url'
134+
assert twitter.authorize_url == 'auth url'
135+
117136
@patch(http_urlopen)
118137
def test_http_request(self, urlopen):
119138
urlopen.return_value = Response(

0 commit comments

Comments
 (0)