When I run this script:
#!/usr/bin/env python
from flask import Flask
from flask.ext.rauth import RauthOAuth2
app = Flask(__name__)
app.config.update(GITHUB_CONSUMER_KEY='foo', GITHUB_CONSUMER_SECRET='bar')
github = RauthOAuth2(
name='github',
base_url='https://api.github.com/',
authorize_url='https://github.com/login/oauth/authorize',
access_token_url='https://github.com/login/oauth/access_token',
)
if __name__ == '__main__':
app.run()
I get this error:
Traceback (most recent call last):
File "rautherr.py", line 13, in <module>
access_token_url='https://github.com/login/oauth/access_token',
File ".env/lib/python2.7/site-packages/flask_rauth.py", line 267, in __init__
OAuth2Service.__init__(self, consumer_key=consumer_key, consumer_secret=consumer_secret, **kwargs)
File ".env/lib/python2.7/site-packages/rauth/service.py", line 318, in __init__
if None in (self.consumer_key, self.consumer_secret):
File ".env/lib/python2.7/site-packages/flask_rauth.py", line 215, in consumer_key
return current_app.config.get(self._consumer_key_config(), None)
File ".env/lib/python2.7/site-packages/werkzeug/local.py", line 336, in __getattr__
return getattr(self._get_current_object(), name)
File ".env/lib/python2.7/site-packages/werkzeug/local.py", line 295, in _get_current_object
return self.__local()
File ".env/lib/python2.7/site-packages/flask/globals.py", line 26, in _find_app
raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context
I expected it to at least instantiate so I could call github.init_app(app)
after that, as in the Twitter example. I tried to fix it by wrapping the instantiation in a with app.app_context():
, but that only changes the error to:
Traceback (most recent call last):
File "rautherr.py", line 14, in <module>
access_token_url='https://github.com/login/oauth/access_token',
File ".env/lib/python2.7/site-packages/flask_rauth.py", line 267, in __init__
OAuth2Service.__init__(self, consumer_key=consumer_key, consumer_secret=consumer_secret, **kwargs)
File ".env/lib/python2.7/site-packages/rauth/service.py", line 318, in __init__
if None in (self.consumer_key, self.consumer_secret):
File ".env/lib/python2.7/site-packages/flask_rauth.py", line 215, in consumer_key
return current_app.config.get(self._consumer_key_config(), None)
File ".env/lib/python2.7/site-packages/flask_rauth.py", line 242, in _consumer_key_config
return '%s_CONSUMER_KEY' % (self.name.upper(),)
AttributeError: 'RauthOAuth2' object has no attribute 'name'
As with issue #1, this seems related to the order in which the objects are configured in rauth 0.4.17. When initializing the RauthOAuth2
instance, OAuth2Service.__init__()
inadvertently calls back into the RauthServiceMixin.consumer_key
property. With no explicitly set app, it assumes it can look up the flask.current_app
and causes the RuntimeError
.
In the latter case, even if the app is set, the consumer_key
access happens before OAuth2Service.__init__()
calls the superimplementation to set the name
attribute, and RauthServiceMixin._consumer_key_config()
raises the AttributeError
.
Downgrading to rauth 0.4.16 causes the error not to occur. I only see it with rauth 0.4.17 and Flask-Rauth 0.3.2. As rauth 0.4.17's change is related to using client_id
instead of consumer_key
etc, this also seems to affect only OAuth2 services (specifically, flask-rauth's Twitter example still works).