Big hat tip to: Udi Milo

These steps will enable a Web2py app that currently authenticates users 
using username/password (or email.password) to add the option for users to 
use their LinkedIn account instead. Once logged in, the app can access the 
LinkedIn API to access user LinkedIn data.

*1. Go to LinkedIn.com and get your developer key and secret.*

*2. Create folder called web2py/linkedin*
Place files: linkedin.py, model.py, helper.py from 
http://code.google.com/p/python-linkedin/
Also create a new file "linkedin_wrapper.py" this will glue LI api into 
Web2py framework
In this add add:
from gluon.http import HTTP
try:
    import linkedin
except ImportError:
    raise HTTP(400,"linkedin module not found")

class LinkedInAccount(object):
    def __init__(self,request,session,key,secret,return_url):
        self.request = request
        self.session = session

        if self.session.linkedin is None:
            self.session.verified = False
            self.session.linkedin = linkedin.LinkedIn(key,secret,return_url, 
gae=request.env.web2py_runtime_gae)
            self.session.linkedin.request_token()

    def verify(self, verifier):
        self.session.verified = verifier and self.session.linkedin.
access_token(verifier = verifier)
        return self.session.verified

    def login_url(self, next="/"):
        return self.session.linkedin.get_authorize_url()

    def logout_url(self, next="/"):
        self.session.linkedin = None
        return next

    def get_user(self):
        if self.session.verified:
            profile = self.session.linkedin.get_profile(fields=['id', 
'first-name', 'last-name','picture-url','industry'])
            return dict(first_name = profile.first_name,
                        last_name = profile.last_name,
                        picture_url = profile.picture_url,
                        industry = profile.industry,
                        username = profile.id)

*3. In dal/db.py*

Just above "auth.define_tables()" add:

# create custom field 'username' for linkedin profile
# the data will be pulled by get_user() in linkedin_wrapper.py
auth.settings.extra_fields['auth_user']= [
    Field('username', writable=False, readable=False),
    Field('picture_url', writable=False, readable=False),
    Field('industry', writable=False, readable=False),]

at the bottom add:
if request.vars.agent == 'linkedin' or session.linkedin:
    from linkedin.linkedAccount import LinkedInAccount
    auth.settings.login_form=LinkedInAccount(request,
                                             session,
                                             [[YOUR_LI_KEY]],
                                             [[YOUR_LI_SECRET]],
                                             'http://' + request.env.http_host 
+ '/[[YOUR_APP]]/default/user/verify')
    
auth.settings.actions_disabled=['register','change_password','request_reset_password','retrieve_username']

*4. In your controller (assumed by dal code above to be called default.py)*
def user():
    if len(request.args)>0 and request.args(0)=='verify':
        auth.settings.login_form.verify(request.vars.oauth_verifier)
        redirect(URL('user', args='login'))

    return dict(form=auth())

In your controller functions you can access LinkedIn API at auth.settings.
login_form.session.linkedin
eg auth.settings.login_form.session.linkedin.get_search()

# this function will enable users to login using their LinkedIn account via 
http://.../[YOUR_APP]/default/agent
# and other users to use their username/password via http://.../[YOUR_APP]
/default/user
def agent():
    redirect(URL('user', args='login', vars=dict(agent='linkedin')))


Post reply
[image: More message actions]
Apr 4

-- 



Reply via email to