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

This doc is based on facebook login in web2py doc:

https://github.com/web2py/web2py/blob/master/gluon/contrib/login_methods/oauth20_account.py

  1. First of all you will need to get from linkedin the key and secret.
  2. Install python-linkedin lib: pip install python-linkedin

 

In the module folder create a new python file, for example linkedin.py:

from gluon.http import HTTP
try:
    from linkedin.linkedin import LinkedInApplication
except ImportError:
    raise HTTP(400, "linkedin module not found")
from gluon.contrib.login_methods.oauth20_account import OAuthAccount
import hashlib
import random

LK_KEY = ''
LK_SECRET = ''
LK_RETURN_URL = 'http://localhost:8000/socialSignIn/default/index'

class LinkedInAccount(OAuthAccount):
    TOKEN_URL="https://www.linkedin.com/uas/oauth2/accessToken"
    AUTH_URL="https://www.linkedin.com/uas/oauth2/authorization"

    def __init__(self):
        OAuthAccount.__init__(self, 'linkedin', LK_KEY, LK_SECRET,
                              self.AUTH_URL, self.TOKEN_URL,
                              scope='r_emailaddress',
                              state=self._make_new_state())

    def _make_new_state(self):
        return hashlib.md5(
            '%s%s' % (random.randrange(0, 2 ** 63), LK_SECRET)).hexdigest()

    def get_user(self):
        if not self.accessToken():
            return None
        app = LinkedInApplication(token=self.accessToken())
        profile = app.get_profile(selectors=['id', 'first-name', 'last-name', 'email-address'])
        if profile:
            if not profile.has_key('username'):
                username = profile['id']
            else:
                username = profile['username']

            if not profile.has_key('emailAddress'):
                email = '%s.fakemail' %(profile['id'])
            else:
                email = profile['emailAddress']

            return dict(first_name = profile['firstName'],
                            last_name = profile['lastName'],
                            username = username,
                            email = '%s' %(email) )

In your model db.py:

from linkedin import LinkedInAccount
auth.settings.login_form=LinkedInAccount()

Related slices

Comments (0)


Hosting graciously provided by:
Python Anywhere