@@ -31,31 +31,63 @@ class LinkedIn(OAuthBackend):
31
31
OAUTH_TYPE = '2.0'
32
32
OAUTH_NAME = 'linkedin'
33
33
OAUTH_CONFIG = {
34
- 'api_base_url' : 'https://api.linkedin.com/v1 /' ,
34
+ 'api_base_url' : 'https://api.linkedin.com/v2 /' ,
35
35
'access_token_url' : 'https://www.linkedin.com/oauth/v2/accessToken' ,
36
36
'authorize_url' : 'https://www.linkedin.com/oauth/v2/authorization' ,
37
37
'client_kwargs' : {
38
- 'scope' : 'r_basicprofile r_emailaddress' ,
38
+ 'scope' : 'r_liteprofile r_emailaddress' ,
39
39
'token_endpoint_auth_method' : 'client_secret_post' ,
40
40
},
41
41
'compliance_fix' : linkedin_compliance_fix
42
42
}
43
43
44
44
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 ))
50
61
resp = self .get (url , ** kwargs )
51
62
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