2016-06-21 71 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