Euphorbium
Euphorbium

Reputation: 1277

The correct way to implement login with google account

What is the correct way to implement user login with google account in web2py? I can not use janrain (for some reason there is no google option when choosing widgets in my account, but google is configured as a provider.)

Upvotes: 1

Views: 646

Answers (2)

Euphorbium
Euphorbium

Reputation: 1277

This is how I did it. Put this in models/db.py and don't forget to define your client_id and client_secret above.

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

Upvotes: 4

PBICS
PBICS

Reputation: 374

Probably should be a comment but I don't have enough points:

Janrain deprecated support for Google's OpenID authentication in early 2015 since Google also deprecated it. Janrain now supports Google+ for authentication and this should be available as a provider in your Janrain Dashboard.

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

If you aren't seeing Google+ as an option then please try contacting Janrain Support at support.janrain.com.

Upvotes: 0

Related Questions