【问题标题】:Problems with Flask-openid in Python 3Python 3 中 Flask-openid 的问题
【发布时间】:2014-09-18 17:52:38
【问题描述】:

我正在尝试让 Flask-openid 工作,但在尝试登录时一直遇到此错误

ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.

使用此功能时会发生这种情况

oid.try_login(openid, ask_for=['email', 'fullname', 'nickname'])

这里是使用函数的地方:

@app.route('/login', methods=['GET', 'POST'])
@oid.loginhandler
def login():
    """Does the login via OpenID.  Has to call into `oid.try_login`
    to start the OpenID machinery.
    """
    # if we are already logged in, go back to were we came from
    if g.user is not None:
        app.logger.info('logged-in: ' + oid.get_next_url())
        return redirect(oid.get_next_url())
    if request.method == 'POST':
        openid = request.form.get('openid_identifier')
        if openid:
            app.logger.info(request.form)
            app.logger.info('logging-in: ' + oid.get_next_url())
            return oid.try_login(openid, ask_for=['email', 'fullname',
                                                  'nickname'])
    app.logger.info('not-logged-in: ' + oid.get_next_url())
    return render_template('login.html', next=oid.get_next_url(),
                           error=oid.fetch_error())

实际上似乎是 Flask-openid 使用的 lxml 的问题:

  File "C:\Python33\lib\site-packages\openid\yadis\etxrd.py", line 69, in parseXRDS
    element = ElementTree.XML(text)
  File "lxml.etree.pyx", line 3012, in lxml.etree.XML (src\lxml\lxml.etree.c:67876) 
  File "parser.pxi", line 1781, in lxml.etree._parseMemoryDocument (src\lxml\lxml.etree.c:102435)

我在 github 上尝试了几个示例项目,但它们都有相同的问题。有什么方法可以让 Flask-openid 在 Python 3 中工作?

【问题讨论】:

    标签: python python-3.x flask openid python-openid


    【解决方案1】:

    我自己只是在学习 Flask,所以帮不上什么忙。

    不过看看http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins

    作者提到

    请注意,由于 Python 2 和 3 之间 unicode 处理的差异,我们必须提供此方法的两个替代版本。

    他使用str 而不是unicode

    def get_id(self):
        try:
            return unicode(self.id)  # python 2
        except NameError:
            return str(self.id)  # python 3
    

    我可能完全错了!在这种情况下,我很抱歉,不过值得一试。

    【讨论】:

      【解决方案2】:

      它不仅仅是字符串。它基于与 Python3 不兼容的较旧的 python-openid 包。有专门针对 Python3 的新版本 python-openid。

      https://pypi.python.org/pypi/python3-openid/3.0.1

      前面提到的同一个博客也详细说明了这一点: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins “不幸的是 Flask-OpenID 1.2.1 版本(当前官方版本)不能很好地与 Python 3 配合使用。通过运行以下命令检查您的版本:”

      【讨论】:

        猜你喜欢
        • 2011-08-05
        • 2015-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-16
        • 2019-10-01
        • 2018-04-04
        • 1970-01-01
        相关资源
        最近更新 更多