2016-06-21 70 views
2

我試圖用flask_oauthlib來訪問我的Twitter的API,但我得到的是錯誤:無法生成請求令牌。這是代碼。Twitter的oauth與flask_oauthlib,無法生成請求令牌

 
    from flask_oauthlib.client import OAuth 
    from flask import Flask, url_for, request, jsonify 

    app = Flask(__name__) 
    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/authorize', 
     consumer_key='dOJjyxB6gxXWTjdtfPUZcZPjl', 
     consumer_secret='im not telling you', 
    ) 


    @app.route('/login') 
    def login(): 
     return twitter.authorize(callback=url_for('authorized', 
                next=request.args.get('next') or request.referrer or None)) 


    @app.route('/authorized') 
    @twitter.authorized_handler 
    def authorized(resp): 
     if resp is None: 
      return 'Access denied: error=%s' % (
       request.args['error'] 
      ) 
     if 'oauth_token' in resp: 
      # session['example_oauth'] = resp 
      print(resp) 
      return jsonify(resp) 
     return str(resp) 


    if __name__ == '__main__': 
     app.run(port=8000, debug=True) 

這在使用http://term.ie/oauth/example/client.php,我得到了一個請求令牌沒有工作。

我自己的靈感與https://github.com/lepture/example-oauth1-server/blob/master/client.pyhttp://flask-oauthlib.readthedocs.io/en/latest/client.html

編輯

怪異的事實:我想這裏的代碼:https://github.com/lepture/flask-oauthlib/blob/master/example/twitter.py 我沒有改變密鑰和密碼,它的工作。

所以我試圖改變他們爲我自己的憑據,並停止工作。我真的不明白...

回答

1

好吧,我發現這個問題。看起來,使用flask-oauthlib時,回調URL是強制性的。所以我添加了一個假的,因爲我仍然在本地主機上,並且它解決了這個問題。

+0

謝謝你分享你的經驗:) – Nabin