【问题标题】:GAE: converting users from users service to oauth2GAE:将用户从用户服务转换为 oauth2
【发布时间】:2015-11-19 06:09:31
【问题描述】:

我目前使用“Google Accounts API”来允许用户登录我的 GAE 应用。所以我使用 users.create_login_url 和 users.get_current_user 并将 ndb.UserProperty 添加到我自己的用户实体中,以便我可以检索该用户的数据。

我现在正在切换到 oauth2(使用自动)。

我需要将我现有的所有用户帐户都转换为 oauth2,我想让我的用户尽可能轻松地完成此操作。这是我目前的计划:

  1. 将登录从用户服务更改为 oauth2。

  2. 用户登录后,它看起来像一个新帐户,用户将看不到他或她以前的数据。

  3. 我将添加一条醒目的消息,要求用户使用旧用户服务登录。

  4. 然后我会将旧用户服务帐户与 oauth2 帐户合并。

这应该可行,但会让用户感到有些困惑。有更好的方法吗?

【问题讨论】:

  • 嗨@RushyPanchal,这两个问题都是我的问题,而且明显不同。不知道为什么你认为它们是一样的......
  • 他们都在询问如何过渡到 OAuth2,@Kekito。它们似乎并没有太大的不同,但我可能是错的。
  • @RushyPanchal,我已经回答了这两个问题。请阅读两者,如果您认为这是一个重复的问题,请告诉我。
  • 我删除了关于重复的评论

标签: python google-app-engine oauth-2.0 google-oauth


【解决方案1】:

我将解释我最终是如何做到这一点的,以防它帮助其他人。

我称我的用户为 manager,我为每个用户都有一个 Manager 实体:

class Manager(ndb.Model):
    user_account = ndb.StructuredProperty(UserAccount))
    linked = ndb.BooleanProperty(default=False)
    user = ndb.UserProperty()

user 属性是我将删除的旧用户服务帐户。 user_account 属性存储信息以识别 Oauth2 帐户:

class UserAccount(ndb.Model):
    provider = ndb.StringProperty(required=True)
    id = ndb.StringProperty(required=True)
    name = ndb.StringProperty()
    email = ndb.StringProperty()

基本上,对于每个经理,我想为user_account(Oauth2 登录)设置一个值并删除user(旧用户帐户)。我希望以最小的经理负担来做到这一点。

当用户最近使用旧用户帐户登录时,该 cookie 仍将处于活动状态。但是,现在用户正在使用 Oauth2 帐户登录。使用 Oauth2 登录后,我们检查旧的用户帐户 cookie 是否仍然处于活动状态。如果是这样,我们会自动合并帐户。这是处理程序的草图。

class ManagerPage(webapp2.RequestHandler):

    def get(self):

        # This returns a Manager entity after the user has logged in with
        # Oauth2. If the user is logging in for the first time, this will
        # be a blank Manager entity.
        self.get_manager()

        # Temporary processing to link accounts.  If the user is still logged
        # as a Google user (because that cookie hasn't expired), then we  
        # automatically transfer their old information to the new Manager 
        # entity.  In doing the conversion below, manager.linked is set to
        # True so this can't happen more than once.  Now that the Manager
        # entity has been updated, redirect back to the same page.
        gae_user = users.get_current_user()
        if not manager.linked and gae_user:
            manager.convert_old_manager(gae_user)
            self.redirect("/manager")

        # Present info to the manager
        ...
        template = JINJA_ENVIRONMENT.get_template("manager.html")
        self.response.write(template.render(template_values))

如果旧用户帐户 cookie 未激活,那么我在上面的管理器页面中有一个链接,要求用户将旧帐户与新帐户链接。当用户使用旧账号登录时,会被重定向到上面的Manager Page,并自动关联账号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 2010-10-30
    • 2015-02-18
    • 2021-12-05
    • 2019-04-09
    • 2013-10-21
    相关资源
    最近更新 更多