我正在使用flask-auth,它提供了一些辅助装饰器。我在下面添加了所有各种方法,但我想问的问题是如何捕捉authorized_handler
装饰器抛出的任何问题。这是一个关于装饰器的一般问题,但我认为一个真实的例子可能会有所帮助。
如果装饰器爆炸了,我怎么能抓住它?
import os
import flask
import flask_oauth
CONSUMER_KEY = os.environ['CONSUMER_KEY']
CONSUMER_SECRET = os.environ['CONSUMER_SECRET']
oauth = flask_oauth.OAuth()
twitter = oauth.remote_app(
'twitter',
base_url='https://api.twitter.com/1/',
request_token_url='https://api.twitter.com/oauth/request_token',
access_token_url='https://api.twitter.com/oauth/access_token',
authorize_url='https://api.twitter.com/oauth/authenticate',
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET
)
app = flask.Flask(__name__)
@app.route('/login')
def login():
return twitter.authorize(
callback=url_for(
'oauth_authorized',
next=request.args.get('next') or request.referrer or None)
)
@app.route('/oauth-authorized')
# what happens if this raises an error?
@twitter.authorized_handler
def oauth_authorized(resp):
print 'foo-bar'