1

I am trying to get Flask-openid working, but keep hitting this error when trying to log in

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

It happens when using this function

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

This is where the function is used:

@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())

and actually seems to be an issue with lxml that Flask-openid uses:

  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)

I have tried a couple of example projects on github, but they all have the same issue. Is there some way I can get Flask-openid to work in Python 3?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
bltpyro
  • 320
  • 3
  • 13

2 Answers2

0

I'm only just learning Flask myself, so I'm not of much help.

However take a look at http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins

The author mentions

Note that due to the differences in unicode handling between Python 2 and 3 we have to provide two alternative versions of this method.

He uses a str instead of unicode

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

I might be completely wrong! In which case i'm sorry, worth a try though.

0

Its much more than just string. Its based on an older python-openid package That is not Python3 compatible. There is a new version of python-openid specifically for Python3.

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

Same blog mentioned earlier also detail this: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins "Unfortunately version 1.2.1 of Flask-OpenID (the current official version) does not work well with Python 3. Check what version you have by running the following command:"