我正在尝试使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在Python3中工作吗?
发布于 2014-11-23 09:10:06
我自己才刚刚开始学习Flask,所以我帮不上什么忙。
但是,让我们看看http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins
作者提到
请注意,由于Python2和Python3之间在unicode处理方面的差异,我们必须提供此方法的两个替代版本。
他使用str而不是unicode
def get_id(self):
try:
return unicode(self.id) # python 2
except NameError:
return str(self.id) # python 3我可能完全错了!在这种情况下,我很抱歉,但值得一试。
发布于 2015-01-16 07:46:44
它不仅仅是字符串。它基于一个不兼容Python3的老版本的python-openid包。有一个新版本的python-openid专门针对Python3。
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版本(当前的官方版本)在Python3上不能很好地工作。通过运行以下命令来检查您的版本:”
https://stackoverflow.com/questions/25919265
复制相似问题