Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Add LinkedIn support courtesy of Kirsten Jones
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeknapp committed Jan 29, 2011
1 parent 0380f9a commit f9b10a0
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
YAHOO = "yahoo"
MYSPACE = "myspace"
DROPBOX = "dropbox"
LINKEDIN = "linkedin"


class OAuthException(Exception):
Expand All @@ -84,6 +85,8 @@ def get_oauth_client(service, key, secret, callback_url):
return MySpaceClient(key, secret, callback_url)
elif service == DROPBOX:
return DropboxClient(key, secret, callback_url)
elif service == LINKEDIN:
return LinkedInClient(key, secret, callback_url)
else:
raise Exception, "Unknown OAuth service %s" % service

Expand Down Expand Up @@ -167,7 +170,7 @@ def encode(text):
return urlencode(params)

def make_async_request(self, url, token="", secret="", additional_params=None,
protected=False, method=urlfetch.GET):
protected=False, method=urlfetch.GET, headers={}):
"""Make Request.
Make an authenticated request to any OAuth protected resource.
Expand All @@ -184,18 +187,19 @@ def make_async_request(self, url, token="", secret="", additional_params=None,
url = "%s?%s" % (url, payload)
payload = None

headers = {"Authorization": "OAuth"} if protected else {}
if protected:
headers["Authorization"] = "OAuth"

rpc = urlfetch.create_rpc(deadline=10.0)
urlfetch.make_fetch_call(rpc, url, method=method, headers=headers,
payload=payload)
return rpc

def make_request(self, url, token="", secret="", additional_params=None,
protected=False, method=urlfetch.GET):
protected=False, method=urlfetch.GET, headers={}):

return self.make_async_request(url, token, secret, additional_params,
protected, method).get_result()
protected, method, headers).get_result()

def get_authorization_url(self):
"""Get Authorization URL.
Expand Down Expand Up @@ -528,3 +532,52 @@ def _lookup_user_info(self, access_token, access_secret):
user_info["country"] = data["country"]

return user_info


class LinkedInClient(OAuthClient):
"""LinkedIn Client.
A client for talking to the LinkedIn API using OAuth as the
authentication model.
"""

def __init__(self, consumer_key, consumer_secret, callback_url):
"""Constructor."""

OAuthClient.__init__(self,
LINKEDIN,
consumer_key,
consumer_secret,
"https://api.linkedin.com/uas/oauth/requestToken",
"https://api.linkedin.com/uas/oauth/accessToken",
callback_url)

def get_authorization_url(self):
"""Get Authorization URL."""

token = self._get_auth_token()
return ("https://www.linkedin.com/uas/oauth/authenticate?oauth_token=%s"
"&oauth_callback=%s" % (token, urlquote(self.callback_url)))

def _lookup_user_info(self, access_token, access_secret):
"""Lookup User Info.
Lookup the user on LinkedIn
"""

user_info = self._get_default_user_info()

# Grab the user's profile from LinkedIn.
response = self.make_request("http://api.linkedin.com/v1/people/~:"
"(picture-url,id,first-name,last-name)",
token=access_token,
secret=access_secret,
protected=False,
headers={"x-li-format":"json"})

data = json.loads(response.content)
user_info = self._get_default_user_info()
user_info["id"] = data["id"]
user_info["picture"] = data["pictureUrl"]
user_info["name"] = data["firstName"] + " " + data["lastName"]
return user_info

0 comments on commit f9b10a0

Please sign in to comment.