google账号登录的正确实现方式

The correct way to implement login with google account

在 web2py 中使用 google 帐户实现用户登录的正确方法是什么?我无法使用 janrain(出于某种原因,在我的帐户中选择小部件时没有 google 选项,但 google 被配置为提供程序。)

可能应该是一个评论,但我没有足够的积分:

Janrain 在 2015 年初弃用了对 Google 的 OpenID 身份验证的支持,因为 Google 也弃用了它。 Janrain 现在支持 Google+ 身份验证,这应该可以在您的 Janrain 仪表板中作为提供者使用。

https://support.janrain.com/hc/communities/public/questions/203662006-Upcoming-changes-to-Google?locale=en-us

如果您没有看到 Google+ 作为选项,请尝试通过 support.janrain.com 联系 Janrain 支持。

我就是这样做的。把它放在 models/db.py 中,不要忘记在上面定义 client_id 和 client_secret。

import json                                                                                                                                     
import urllib2
from gluon.contrib.login_methods.oauth20_account import OAuthAccount

class googleAccount(OAuthAccount):
    AUTH_URL="https://accounts.google.com/o/oauth2/auth"
    TOKEN_URL="https://accounts.google.com/o/oauth2/token"

    def __init__(self):
        OAuthAccount.__init__(self,
                                client_id=client_id,
                                client_secret=client_secret,
                                auth_url=self.AUTH_URL,
                                token_url=self.TOKEN_URL,
    approval_prompt='force', state='auth_provider=google',
    scope='https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email')

    def get_user(self):
        token = self.accessToken()
        if not token:
            return None

        uinfo_url = 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=%s' % urllib2.quote(token, safe='')
        uinfo = None
        try:
            uinfo_stream = urllib2.urlopen(uinfo_url)
        except:
            session.token = None
            return
        data = uinfo_stream.read()
        uinfo = json.loads(data)
        return dict(first_name = uinfo['given_name'],
                        last_name = uinfo['family_name'],
                        username = uinfo['id'], email=uinfo['email'])


auth.settings.actions_disabled=['register',
    'change_password','request_reset_password','profile']
auth.settings.login_form=googleAccount()