Thank you. I got the code to compile by running web2py from source but the 
suggested code above did not exactly work. I found a working solution from 
stackoverflow.
http://stackoverflow.com/questions/30072099/the-correct-way-to-implement-login-with-google-account

On Sunday, July 12, 2015 at 4:15:07 AM UTC-5, Massimo Di Pierro wrote:
>
> You have to run web2py from source and run 
>
> pip install oath2
>
> On Saturday, 11 July 2015 08:24:03 UTC-5, Do-Yang Kim wrote:
>>
>> Hi. I'm a newbie trying web2py. I get this error when I try above code
>>
>>
>> *Cannot import module 'applications.admin.modules.oauth2'*
>>
>> I'm assuming that I don't have oauth2 library set up in the module 
>> section. How can I fix this?
>>
>> On Thursday, April 9, 2015 at 7:30:46 AM UTC-5, piero crisci wrote:
>>>
>>> First of all you need to get the *google_auth.json file to use OAuth*
>>> To get that you need to register your Google account as a webdeveloper
>>> You can find how get the information on Google :)
>>>  
>>> Then u can change ur auth table  in this way
>>>  
>>> In the model.py
>>>  
>>> from gluon.tools import Auth, Crud, Service, PluginManager, prettydate
>>> auth = Auth(db,secure=True)
>>> crud, service, plugins = Crud(db), Service(), PluginManager()
>>> ## ------------TABELLE USER ------------------- ##
>>> ## create all tables needed by auth if not custom tables
>>> auth.settings.extra_fields['auth_user']= [
>>>   Field('phone', type='string', label='Telefono'),
>>>   Field('country',type='string', label='Nazione'),
>>>   Field('city',type='string', label='Città'),
>>>   Field('address',type='string', label='Indirizzo'),
>>>   Field('auth_login', type='string', default='basic', label='Tipo 
>>> Login',readable=False, writable=False),
>>>   Field('url_img', requires=IS_EMPTY_OR(IS_URL()), label='Link Immagine 
>>> Profile',readable=False, writable=False),
>>>   Field('nickname',  type='string', label='Nickname'),
>>>   Field('birthdate',  type='date', label='Data Di nascita'),
>>>   Field('gender', type = 'string', label='Genere' ,requires = 
>>> IS_IN_SET(['M','F']), default = 'M'),
>>>   Field('facebook_id', type='string', label='Username di 
>>> Facebook',readable=False, writable=False),
>>>   Field('twitter_id',  type='string', label='Username di 
>>> Twitter',readable=False, writable=False),
>>>   Field('google_id',  type='string', label='Username di 
>>> Google',readable=False, writable=False ),
>>>   Field('linkedin_id',  type='string', label='Username di 
>>> Linkedin',readable=False, writable=False),
>>>   ]
>>> auth.define_tables(username=False, signature=True)
>>>  
>>> *In The Controller*
>>>  
>>> Import AccountAccess
>>> def google():
>>>     if auth.is_logged_in():
>>>         redirect(URL(r=request, c='default', f='index'))
>>>     folder = request.folder
>>>     google_access = AccountAccess.GoogleAccount(folder)
>>>     auth.settings.login_form=google_access
>>>  
>>>     return auth.login(next=URL(r=request, c='default', f='index'))
>>>  
>>>  
>>> In the Module section
>>> Create the *AccountAccess* module
>>>
>>> import oauth2 as oauth
>>> from gluon.contrib.login_methods.oauth10a_account import OAuthAccount as 
>>> OAuthAccount10a
>>> from gluon.contrib.login_methods.oauth20_account import OAuthAccount
>>> from oauthtwitter_account import OAuthAccount as OauthAccountTwitter
>>> import os
>>> import storage
>>> import urllib2
>>> from oauth2 import Client, Consumer, Token
>>>
>>> class GoogleAccount(OAuthAccount):
>>>     "OAuth 2.0 for Google"
>>>     def __init__(self,db,session,request,response,folder):
>>>         with open(os.path.join(folder, *'private/google_auth.json'*), 
>>> 'rb') as f:
>>>             gai = storage.Storage(json.load(f)['web'])
>>>         self.db = db
>>>         self.request = request
>>>         self.response = response
>>>         self.session = session
>>>         g = dict(
>>>                 request=request,
>>>                 response=response,
>>>                 session=session,
>>>                 )
>>>         OAuthAccount.__init__(self,g, gai.client_id, gai.client_secret,
>>>                               gai.auth_uri, gai.token_uri,
>>>                               scope='
>>> https://www.googleapis.com/auth/userinfo.profile 
>>> https://www.googleapis.com/auth/userinfo.email 
>>> https://www.googleapis.com/auth/plus.login',
>>>                               approval_prompt='auto',
>>>                               access_type = 'offline',
>>>                               state="auth_provider=google")
>>>     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 None
>>>         data = uinfo_stream.read()
>>>         uinfo = json.loads(data)
>>>         username = uinfo['id']
>>>         if uinfo:
>>>             gender = 'M'
>>>             if uinfo['gender'][0].lower() == 'f':
>>>                 gender = 'F'
>>>             existent = self.db(self.db.auth_user.email == 
>>> uinfo["email"]).select(self.db.auth_user.id
>>> ,self.db.auth_user.auth_login).first()
>>>             if existent:
>>>                 if existent.auth_login <> 'Google':
>>>                     diz_account = dict(
>>>                          username = uinfo['email'],
>>>                          gender = gender,
>>>                          auth_login = 'Google',
>>>                          url_img = uinfo.get('picture', ''),
>>>                          google_id = uinfo['id'],
>>>                          registration_id = uinfo['id']
>>>                     )
>>>                     existent.update_record(**diz_account)
>>>                 return dict(first_name = uinfo.get('given_name', 
>>> uinfo["name"].split()[0]),
>>>                             last_name = uinfo.get('family_name', 
>>> uinfo["name"].split()[-1]),
>>>                             username = uinfo['email'],
>>>                             email = uinfo['email'],
>>>                             gender = gender,
>>>                             auth_login = 'Google',
>>>                             birthdate = uinfo.get('birthday', ''),
>>>                             url_img = uinfo.get('picture', ''),
>>>                             google_id = uinfo['id'],
>>>                             registration_id = uinfo['id']
>>>                             )
>>>             else:
>>> #                self.db.auth.send_welcome_email(user)
>>>                 return dict(first_name = uinfo.get('given_name', 
>>> uinfo["name"].split()[0]),
>>>                             last_name = uinfo.get('family_name', 
>>> uinfo["name"].split()[-1]),
>>>                             username = uinfo['email'],
>>>                             email = uinfo['email'],
>>>                             gender = gender,
>>>                             auth_login = 'Google',
>>>                             birthdate = uinfo.get('birthday', ''),
>>>                             url_img = uinfo.get('picture', ''),
>>>                             google_id = uinfo['id'],
>>>                             registration_id = uinfo['id']
>>>                             )
>>>     def call_api(self):
>>>         api_return = urllib.urlopen("
>>> https://www.googleapis.com/oauth2/v1/userinfo?access_token=%s"; % 
>>> self.accessToken())
>>>         user = json.loads(api_return.read())
>>>         if user:
>>>             return user
>>>         else:
>>>             session.token = None
>>>             return None
>>>  
>>>  
>>> It worked for me 
>>>  
>>>
>>> Il giorno martedì 7 aprile 2015 16:01:22 UTC+2, Moiz Nagpurwala ha 
>>> scritto:
>>>
>>>> Hello,
>>>>
>>>> Still waiting for a working example of OAuth2 with Google.
>>>>
>>>> It is very crucial for my application to integrate Google 
>>>> authentication in order to succeed.
>>>>
>>>> Hope this great community won't let me down.
>>>>
>>>> Thanks and regards.
>>>>
>>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to