[英]Python3 Flask Twitter OAuth
I've been trying to use twitter with Flask OAuth and im running Python 3. I've followed this tutorial: http://pythonhosted.org/Flask-OAuth/ and tried some workarrounds but it doesn't work. 我一直在尝试将Twitter与Flask OAuth配合使用,并在运行Python 3的即时消息中使用。我遵循了本教程: http ://pythonhosted.org/Flask-OAuth/,并尝试了一些工作环境,但它不起作用。
This is what I've got so far: 到目前为止,这是我得到的:
from flask import *
from flask_oauthlib.client import OAuth
from User import User
oauth = OAuth()
twitter = oauth.remote_app(
base_url='https://api.twitter.com/1.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/authorize',
consumer_key='My Consumer Code',
consumer_secret='My Secret Code',
name='twitter'
)
app = Flask(__name__)
current_user = User()
@app.route('/')
def home_show():
return render_template('home.html')
@app.route('/about')
def about_show():
return render_template('about.html')
@app.route('/app')
def app_show():
if not current_user.is_authenticated():
return redirect('/')
else:
return twitter.authorize(callback=url_for('oauth_authorized',
next=request.args.get('next') or request.referrer or None))
@twitter.tokengetter
def get_twitter_token(token = None):
if current_user.is_authenticated():
return session.get('twitter_token')
else:
return None
@app.route('/oauth-authorized', methods=['GET'])
@twitter.authorized_handler
def oauth_authorized(resp):
next_url = request.args.get('next') or url_for('index')
if resp is None:
flash(u'You denied the request to sign in.')
return redirect(next_url)
session['twitter_token'] = (
resp['oauth_token'],
resp['oauth_token_secret']
)
session['twitter_user'] = resp['screen_name']
flash('You were signed in as %s' % resp['screen_name'])
return redirect(next_url)
if __name__ == '__main__':
app.run(debug=True)
It crashes on 它崩溃了
return twitter.authorize(callback=url_for('oauth_authorized',next=request.args.get('next') or request.referrer or None))
and this is the trace: 这是跟踪:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/Karl/Python/tweettosleep/hello.py", line 40, in app_show
return twitter.authorize(callback=url_for('oauth_authorized',next=request.args.get('next') or request.referrer or None))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask_oauthlib/client.py", line 455, in authorize
token = self.generate_request_token(callback)[0]
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask_oauthlib/client.py", line 527, in generate_request_token
type='token_generation_failed'
flask_oauthlib.client.OAuthException: Failed to generate request token
I've already found the answer, there is actually nothing wrong with the code. 我已经找到答案了,代码实际上没有错。 Sorry if it's a little bit messed up. 对不起,如果有点混乱。 The problem was that my consumer_key for twitter isn't valid. 问题是我的twitter的Consumer_key无效。 I have no idea why. 我不知道为什么。 Still figuring out. 仍在弄清楚。
So the point is, if you get the same Traceback, there might be a possibility that your twitter consumer_key is not correct! 因此,要点是,如果您获得相同的Traceback,则您的Twitter Consumer_key可能不正确!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.