2014-04-11 107 views
1

我一直在嘗試使用twitter與Flask OAuth和即時通訊運行Python 3.我按照本教程:http://pythonhosted.org/Flask-OAuth/並嘗試了一些workarrounds,但它不起作用。Python3 Flask Twitter OAuth

這是我到目前爲止有:

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) 

它崩潰的

return twitter.authorize(callback=url_for('oauth_authorized',next=request.args.get('next') or request.referrer or None)) 

,這是跟蹤:

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 
+0

** **如何不起作用?請提供更多細節。目前很難說出你在問什麼。 –

+0

這個想法是從app_show方法中取回一個令牌並且不起作用。問題是我不能生成請求令牌。讓我編輯它並添加跟蹤。 –

+0

[flask-oauth](http://pythonhosted.org/Flask-OAuth/)和[flask-oauthlib](http://flask-oauthlib.readthedocs.org/en/latest/index.html)是不同的庫。你提到'flask-oauth',但實際上是從'flask-oauthlib'進口的。你可以看到'flask-oauthlib'的示例代碼[here](http://flask-oauthlib.readthedocs.org/en/latest/client.html)。 – vivekagr

回答

0

我已經找到了答案,代碼實際上沒有錯。對不起,如果有點混亂。問題是我的twitter的consumer_key無效。我不知道爲什麼。還在搞清楚。

所以問題是,如果你得到相同的Traceback,可能有可能你的twitter的consumer_key不正確!