Skip to content

Commit c9f9cb4

Browse files
xofbdlepture
authored andcommitted
Update to LinkedIn v2 (#40)
1 parent 9955593 commit c9f9cb4

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

loginpass/linkedin.py

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,63 @@ class LinkedIn(OAuthBackend):
3131
OAUTH_TYPE = '2.0'
3232
OAUTH_NAME = 'linkedin'
3333
OAUTH_CONFIG = {
34-
'api_base_url': 'https://api.linkedin.com/v1/',
34+
'api_base_url': 'https://api.linkedin.com/v2/',
3535
'access_token_url': 'https://www.linkedin.com/oauth/v2/accessToken',
3636
'authorize_url': 'https://www.linkedin.com/oauth/v2/authorization',
3737
'client_kwargs': {
38-
'scope': 'r_basicprofile r_emailaddress',
38+
'scope': 'r_liteprofile r_emailaddress',
3939
'token_endpoint_auth_method': 'client_secret_post',
4040
},
4141
'compliance_fix': linkedin_compliance_fix
4242
}
4343

4444
def profile(self, **kwargs):
45-
fields = [
46-
'id', 'email-address', 'picture-url', 'public-profile-url',
47-
'formatted-name', 'first-name', 'last-name', 'maiden-name',
48-
]
49-
url = 'people/~:({})?format=json'.format(','.join(fields))
45+
user_name = self.get_user_name(**kwargs)
46+
user_email = self.get_user_email(**kwargs)
47+
48+
params = {
49+
'sub': user_name['id'],
50+
'given_name': user_name['firstName'],
51+
'family_name': user_name['lastName'],
52+
'name': user_name['firstName'] + ' ' + user_name['lastName'],
53+
'email': user_email[0],
54+
}
55+
56+
return UserInfo(params)
57+
58+
def get_user_name(self, **kwargs):
59+
fields = ['id', 'firstName', 'lastName']
60+
url = 'me?projection=({})'.format(','.join(fields))
5061
resp = self.get(url, **kwargs)
5162
resp.raise_for_status()
52-
return UserInfo(map_profile_fields(resp.json(), {
53-
'sub': 'id',
54-
'email': 'emailAddress',
55-
'name': 'formattedName',
56-
'given_name': 'firstName',
57-
'family_name': 'lastName',
58-
'middle_name': 'maidenName',
59-
'picture': 'pictureUrl',
60-
'profile': 'publicProfileUrl',
61-
}))
63+
user_id = resp.json()['id']
64+
fname_data = resp.json()['firstName']
65+
lname_data = resp.json()['lastName']
66+
67+
def localized_key(name):
68+
return '{}_{}'.format(
69+
name['preferredLocale']['language'],
70+
name['preferredLocale']['country']
71+
)
72+
73+
first_name_locale = localized_key(fname_data)
74+
last_name_locale = localized_key(lname_data)
75+
76+
return {
77+
'id': user_id,
78+
'firstName': fname_data['localized'].get(first_name_locale, ''),
79+
'lastName': lname_data['localized'].get(last_name_locale, '')
80+
}
81+
82+
def get_user_email(self, **kwargs):
83+
url = 'emailAddress?q=members&projection=(elements*(handle~))'
84+
resp = self.get(url, **kwargs)
85+
resp.raise_for_status()
86+
87+
emails = []
88+
for el in resp.json().get('elements', []):
89+
email = el.get('handle~', {}).get('emailAddress')
90+
if email is not None:
91+
emails.append(email)
92+
93+
return emails

0 commit comments

Comments
 (0)