Python self.auth.get_user_by_password在创建帐户后尝试登录用户时引发InvalidAuthIdError

Python self.auth.get_user_by_password在创建帐户后尝试登录用户时引发InvalidAuthIdError,python,python-2.7,webapp2,Python,Python 2.7,Webapp2,在我的用户注册处理程序中,我有如下代码: success, info = self.auth.store.user_model.create_user( "auth:" + username, password_raw = password) if success: self.auth.get_user_by_password("auth:" + username, password) 用户名是在处理程序的前面从请求的PO

在我的用户注册处理程序中,我有如下代码:

success, info = self.auth.store.user_model.create_user(
                "auth:" + username,
                password_raw = password)
if success:
    self.auth.get_user_by_password("auth:" + username, password)
用户名是在处理程序的前面从请求的POST参数中获得的

用户提供有效的用户名(一个还不存在的用户名)、有效的密码,此代码将被执行,并且它总是导致auth.InvalidAuthIdError()。当我检查数据存储时,我可以看到已使用指定的身份验证id创建了一个用户

我花了很多时间来解决这个问题,其中有一个方面非常令人费解。如果我事先知道,我将提交用户名等于“someusername”的用户的注册表,并将此用户名硬编码为get_user_by_密码,如下所示:

self.auth.get_user_by_password("auth:someusername", password)
这似乎可以让用户正确登录,并且不会引发InvalidAuthIdError。我不知道如何进一步进行故障排除。。。“auth:someusername”与“auth:”+“someusername”有何不同

这是我得到的回溯:

Traceback (most recent call last):
  File "/home/radek/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "/home/radek/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "/home/radek/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/radek/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/home/radek/prasowalnia/main.py", line 44, in dispatch
    super(BaseHandler, self).dispatch()
  File "/home/radek/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/home/radek/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/home/radek/prasowalnia/main.py", line 199, in post
    self.auth.get_user_by_password("auth:"+username, password)
  File "/home/radek/google_appengine/lib/webapp2-2.5.1/webapp2_extras/auth.py", line 459, in get_user_by_password
    silent=silent)
  File "/home/radek/google_appengine/lib/webapp2-2.5.1/webapp2_extras/auth.py", line 278, in validate_password
    return self.get_user_by_auth_password(auth_id, password, silent=silent)
  File "/home/radek/google_appengine/lib/webapp2-2.5.1/webapp2_extras/auth.py", line 150, in get_user_by_auth_password
    user = self.user_model.get_by_auth_password(auth_id, password)
  File "/home/radek/google_appengine/lib/webapp2-2.5.1/webapp2_extras/appengine/auth/models.py", line 298, in get_by_auth_password
    raise auth.InvalidAuthIdError()
InvalidAuthIdError
编辑:这非常奇怪,但如果我将代码修改为:

if success:
    time.sleep(1)
    self.auth.get_user_by_password("auth:"+username, password)
if success:
    counter = 0
    while True:
        try:
            counter += 1
            self.auth.get_user_by_password("auth:"+username, password)
        except:
            continue
        else:
            break
    self.write(counter)
一切正常,不会抛出错误

编辑:有点不对劲,好像我将代码修改为:

if success:
    time.sleep(1)
    self.auth.get_user_by_password("auth:"+username, password)
if success:
    counter = 0
    while True:
        try:
            counter += 1
            self.auth.get_user_by_password("auth:"+username, password)
        except:
            continue
        else:
            break
    self.write(counter)
我的计数器略高于或低于20。在生产中如何实现这一点?我是否应该期望self.auth.store.user\u model.create\u user()可以返回,而不需要在数据存储中实际完成用户的创建?我是否应该对针对数据存储发布的任何put()进行此假设?如果有人对此有任何解释,我们将不胜感激!谢谢大家!


编辑:读一读这篇文章,我猜我面临这个问题是因为“最终一致的查询”。我可能会从create_user方法返回的信息中获取新用户的密钥,但是webapp2似乎没有提供一种通过数据存储中的密钥检索用户的方法。

下面应该讨论最终一致性带来的问题

success, info = self.auth.store.user_model.create_user("auth:" + username,
                                                       password_raw = password)
if success:
    self.auth.set_session(self.auth.store.user_to_dict(info), remember=True)
它应该在不查询ndb的情况下登录用户。因为我是webapp2新手,所以我将了解这是如何实现的,如果有任何问题,我将在这里添加信息


我一直在关注这篇博文:虽然不确定为什么其他人没有遇到过与我相同的问题。

你并不孤单——我也有同样的问题!(我没有尝试你提到的“硬编码用户名”的东西,但是-和你一样-我发现我无法在注册后立即使用
get\u user\u by\u password
)登录)你的答案正是我需要的。谢谢