If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

 

first thing, go to linkedIn.com and get your key and secret. after getting it:
 
1. add linkedIn.py and model.py to Modules from the python-linkedin lib
2. add the linkedinAccount.py to modules. modify it to look like:
 
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=True)
            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. add the following to your db.py
 
## LinkedIn
auth.settings.actions_disabled=['register','change_password','request_reset_password','retrieve_username']
from linkedinAccount import LinkedInAccount
auth.settings.login_form=LinkedInAccount(
    request,
    session,
    KEY,
    SECRET,
    'http://' + request.env.http_host + '/user/verify')

and just above "auth.define_tables()" add this:

## create custom field 'username' for linkedin authentication
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),
]
 
4. add the following to your default.py, the user function (clear is a debug tool, in case your session expires or you play with it and you need to remove it)
 
def user():
    if len(request.args)>0 and request.args(0)=='verify':
        auth.settings.login_form.verify(request.vars.oauth_verifier)
        redirect(URL('user', 'login'))

    if request.args(0)=='clear':
        session.linkedin = None
        return 'clean'

 

This recipe was posted by Udi Milo http://about.me/udimilo

Related slices

Comments (0)


Hosting graciously provided by:
Python Anywhere