11import requests
2+ from requests_oauthlib import OAuth1
23import urllib
34import random
45import hashlib
5253 VIRAL = 'VIRL' )
5354
5455
56+ class LinkedInDeveloperAuthentication (object ):
57+ """
58+ Uses all four credentials provided by LinkedIn as part of an OAuth 1.0a flow that provides instant
59+ API access with no redirects/approvals required. Useful for situations in which users would like to
60+ access their own data or during the development process.
61+ """
62+
63+ def __init__ (self , consumer_key , consumer_secret , user_token , user_secret , redirect_uri , permissions = []):
64+ self .consumer_key = consumer_key
65+ self .consumer_secret = consumer_secret
66+ self .user_token = user_token
67+ self .user_secret = user_secret
68+ self .redirect_uri = redirect_uri
69+ self .permissions = permissions
70+
5571class LinkedInAuthentication (object ):
72+ """
73+ Implements a standard OAuth 2.0 flow that involves redirection for users to authorize the application
74+ to access account data.
75+ """
76+
5677 def __init__ (self , key , secret , redirect_uri , permissions = []):
5778 self .AUTHORIZATON_URL = 'https://www.linkedin.com/uas/oauth2/authorization'
5879 self .ACCESS_TOKEN_URL = 'https://www.linkedin.com/uas/oauth2/accessToken'
@@ -124,7 +145,7 @@ def parse(cls, selector):
124145class LinkedInApplication (object ):
125146 def __init__ (self , authentication ):
126147 assert authentication , 'Authentication instance must be provided'
127- assert type (authentication ) == LinkedInAuthentication , 'Auth type mismatch'
148+ assert type (authentication ) in ( LinkedInAuthentication , LinkedInDeveloperAuthentication ) , 'Auth type mismatch'
128149 self .BASE_URL = 'https://api.linkedin.com'
129150 self .authentication = authentication
130151
@@ -138,14 +159,24 @@ def make_request(self, method, url, data=None, params=None, headers=None,
138159 else :
139160 headers .update ({'x-li-format' : 'json' , 'Content-Type' : 'application/json' })
140161
141- if params is None :
142- params = {'oauth2_access_token' : self .authentication .token .access_token }
143- else :
144- params ['oauth2_access_token' ] = self .authentication .token .access_token
162+
163+ kw = dict (data = data , params = params ,
164+ headers = headers , timeout = timeout )
145165
146- return requests .request (method .upper (), url , data = data , params = params ,
147- headers = headers , timeout = timeout )
166+ if isinstance (self .authentication , LinkedInDeveloperAuthentication ):
167+ # Let requests_oauthlib.OAuth1 to do *all* of the work here
168+ auth = OAuth1 (self .authentication .consumer_key , self .authentication .consumer_secret ,
169+ self .authentication .user_token , self .authentication .user_secret )
170+ kw .update ({'auth' : auth })
171+ else :
172+ if params is None :
173+ params = {'oauth2_access_token' : self .authentication .token .access_token }
174+ else :
175+ params ['oauth2_access_token' ] = self .authentication .token .access_token
176+
177+ return requests .request (method .upper (), url , ** kw )
148178
179+
149180 def get_profile (self , member_id = None , member_url = None , selectors = None ,
150181 params = None , headers = None ):
151182 if member_id :
0 commit comments