python - Flask-Social Twitter 登录失败并显示消息 - OAuthException : Failed to generate request token

标签 python flask twitter-oauth flask-login

我无法使用 Flask-Social ( https://pythonhosted.org/Flask-Social/ ) 登录 Facebook 或 Twitter,请按照此处的示例: http://flask-social-example.herokuapp.com/

重点关注 Twitter 案例,我在登录 HTML 页面上调用 Flask-social,如下所示: {{social_login('twitter', 'Twitter')}}

并在注册 HTML 页面上使用:
{{social_register('twitter', 'Twitter')}}

这是我在views.py中的代码:

@app.route('/register', methods=['GET', 'POST'])
@app.route('/register/<provider_id>', methods=['GET', 'POST'])
def register(provider_id=None):
    if current_user.is_authenticated():
        return redirect('/landing')

    form = RegisterForm()

    if provider_id:
        print("provider!!")

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

    else:
        print("no provider!!")
        provider = None
        connection_values = None

    if form.validate_on_submit():
        print("enter validate on submit")
        ds = app.security.datastore
        user = ds.create_user(email=form.email.data, password=form.password.data)
        ds.commit()

        # See if there was an attempted social login prior to registering
        # and if so use the provider connect_handler to save a connection
        connection_values = session.pop('failed_login_connection', None)

        if connection_values:
            print("enter connection_values")
            connection_values['user_id'] = user.id
            connect_handler(connection_values, provider)

        if login_user(user):
            ds.commit()
            flash('Account created successfully', 'info')
            return redirect(url_for('rightform'))

        return render_template('thanks.html', user=user)

    login_failed = int(request.args.get('login_failed', 0))

    return render_template('register.html',
                           form=form,
                           provider=provider,
                           login_failed=login_failed,
                           connection_values=connection_values)

这会产生以下跟踪:

  Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python2.7/dist-packages/flask_security/decorators.py", line 205, in wrapper
    return f(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/flask_social/views.py", line 49, in login
    return provider.authorize(callback_url)
  File "/usr/local/lib/python2.7/dist-packages/flask_oauth.py", line 331, in authorize
    token = self.generate_request_token(callback)[0]
  File "/usr/local/lib/python2.7/dist-packages/flask_oauth.py", line 299, in generate_request_token
    type='token_generation_failed')
OAuthException: Failed to generate request token

我非常确定这对 secret 工作正常,因为我可以使用 Flask-OAuth 并收到不同的错误,表明我至少已经到达回调 URL 位。

最佳答案

我认为你需要在twitter应用程序中设置回调url,例如你的应用程序是

http://myapp.com

回调网址应该是

http://myapp.com/login/twitter

再见!

关于python - Flask-Social Twitter 登录失败并显示消息 - OAuthException : Failed to generate request token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25066649/

相关文章:

Python strftime 德语日期字符串

python - Flask - 无法访问根地址的静态文件

python - 如何传递包含 url 的内容? ,/, & 里面的flask webservice方法

PHP 推特 OAuth 转推

python - 在 scikit 学习中从 LDA 获取主题词分布

python - 如何根据列表中的值在字典列表中查找字典?

python - Wtforms,多选文件上传

twitter - Twitter4j API 教程

node.js - 如何在 Twitter 中注册 webhook url?

python - Matplotlib 在 .set_aspect = 'equal' 时删除白色边框(填充)