首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现google智能锁一键登录

如何实现google智能锁一键登录
EN

Stack Overflow用户
提问于 2020-07-11 19:30:39
回答 1查看 254关注 0票数 0

我想在文档https://developers.google.com/identity/one-tap/web/的帮助下,在我的网站上实现谷歌的一键注册和自动登录,但我对如何在python中实现感到困惑。

代码语言:javascript
复制
def smartlock(request):
    try:
        CLIENT_ID='*******'
        csrf_token_cookie = self.request.cookies.get('g_csrf_token')
        if not csrf_token_cookie:
            webapp2.abort(400, 'No CSRF token in Cookie.')
        csrf_token_body = self.request.get('g_csrf_token')
        if not csrf_token_body:
            webapp2.abort(400, 'No CSRF token in post body.')
        if csrf_token_cookie != csrf_token_body:
            webapp2.abort(400, 'Failed to verify double submit cookie.')
        # Specify the CLIENT_ID of the app that accesses the backend:
        idinfo = id_token.verify_oauth2_token(csrf_token_cookie, requests.Request(), CLIENT_ID)

        # Or, if multiple clients access the backend server:
        # idinfo = id_token.verify_oauth2_token(token, requests.Request())
        # if idinfo['aud'] not in [CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]:
        #     raise ValueError('Could not verify audience.')

        if idinfo['iss'] not in ['accounts.google.com', 'https://accounts.google.com']:
            raise ValueError('Wrong issuer.')

        # If auth request is from a G Suite domain:
        # if idinfo['hd'] != GSUITE_DOMAIN_NAME:
        #     raise ValueError('Wrong hosted domain.')

        # ID token is valid. Get the user's Google Account ID from the decoded token.
        userid = idinfo['sub']
    except ValueError:
        # Invalid token
        pass
'''
EN

回答 1

Stack Overflow用户

发布于 2020-07-16 01:29:57

正如this page的“关键点”部分所述: ID令牌在credential字段中返回,而不是在g_csrf_token字段中返回。

因此,您需要通过如下代码获取idinfo:

代码语言:javascript
复制
credential = self.request.get('credential')

idinfo = id_token.verify_oauth2_token(credential, requests.Request(), CLIENT_ID)

g_csrf_token参数有不同的用途。确保请求来自您自己域名内的页面,防止跨站请求伪造攻击。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62848597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档