diff --git a/docs/client.rst b/docs/client.rst index c1d1bfed..50f27bab 100644 --- a/docs/client.rst +++ b/docs/client.rst @@ -65,6 +65,11 @@ Flask config by the key ``TWITTER``, the configuration looks like:: oauth.init_app(app) +Or looks like that:: + + app.config['TWITTER_CONSUMER_KEY'] = 'a random string key' + app.config['TWITTER_CONSUMER_SECRET'] = 'a random string secret' + Twitter can get consumer key and secret from the Flask instance now. You can put all the configuration in ``app.config`` if you like, which diff --git a/flask_oauthlib/client.py b/flask_oauthlib/client.py index fc21d0c4..6171228b 100644 --- a/flask_oauthlib/client.py +++ b/flask_oauthlib/client.py @@ -297,10 +297,18 @@ def _get_property(self, key, default=False): return default return attr app = self.oauth.app or current_app - config = app.config[self.app_key] - if default is not False: - return config.get(key, default) - return config[key] + if self.app_key in app.config: + # works with dict config + config = app.config[self.app_key] + if default is not False: + return config.get(key, default) + return config[key] + else: + # works with plain text config + config_key = "%s_%s" % (self.app_key, key.upper()) + if default is not False: + return app.config.get(config_key, default) + return app.config[config_key] def make_client(self, token=None): # request_token_url is for oauth1 diff --git a/tests/test_client.py b/tests/test_client.py index 1c8468eb..db7e820f 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -114,6 +114,25 @@ def test_lazy_load(self): assert twitter.authorize_url == 'auth url' assert twitter.content_type is None + def test_lazy_load_with_plain_text_config(self): + oauth = OAuth() + twitter = oauth.remote_app('twitter', app_key='TWITTER') + + app = Flask(__name__) + app.config['TWITTER_CONSUMER_KEY'] = 'twitter key' + app.config['TWITTER_CONSUMER_SECRET'] = 'twitter secret' + app.config['TWITTER_REQUEST_TOKEN_URL'] = 'request url' + app.config['TWITTER_ACCESS_TOKEN_URL'] = 'token url' + app.config['TWITTER_AUTHORIZE_URL'] = 'auth url' + + oauth.init_app(app) + + assert twitter.consumer_key == 'twitter key' + assert twitter.consumer_secret == 'twitter secret' + assert twitter.request_token_url == 'request url' + assert twitter.access_token_url == 'token url' + assert twitter.authorize_url == 'auth url' + @patch(http_urlopen) def test_http_request(self, urlopen): urlopen.return_value = Response(