2014-04-11 4 views
1

저는 Flask OAuth와 twitter를 사용하려고했으며 파이썬 3을 실행 중입니다. http://pythonhosted.org/Flask-OAuth/을 따라 작업 해봤지만 작동하지 않습니다. 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

나는 이미 발견했습니다 대답은 실제로 코드에 아무런 문제가 없습니다. 죄송합니다. 좀 엉망이되면. 문제는 내 consumer_key 트위터가 유효하지 않다는 것입니다. 나는 이유를 모른다. 아직도 알아 내고있어.

그렇다면 동일한 Traceback을 얻는다면 트위터 consumer_key가 올바르지 않을 수도 있습니다.