Script to log in to LinkedIn API.

This commit is contained in:
Chris Rossi
2013-12-13 16:04:09 -05:00
committed by Diana Huang
parent b2db05fc89
commit 411d39deea
2 changed files with 81 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
"""
Log into LinkedIn API.
"""
import json
import urllib2
import urlparse
import uuid
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from ...models import LinkedInTokens
class Command(BaseCommand):
"""
Can take a sysadmin through steps to log into LinkedIn API so that the
findusers script can work.
"""
args = ''
help = ('Takes a user through the steps to log in to LinkedIn as a user '
'with API access in order to gain an access token for use by the '
'findusers script.')
def handle(self, *args, **options):
"""
"""
api = getattr(settings, "LINKEDIN_API", None)
if not api:
raise CommandError("LINKEDIN_API is not configured")
state = str(uuid.uuid4())
url= ("https://www.linkedin.com/uas/oauth2/authorization"
"?response_type=code"
"&client_id=%s&state=%s&redirect_uri=%s" % (
api['CLIENT_ID'], state, api['REDIRECT_URI']))
print "Let's log into your LinkedIn account."
print "Start by visiting this url:"
print url
print
print "Within 30 seconds of logging in, enter the full URL of the "
print "webpage you were redirected to: "
redirect = raw_input()
query = urlparse.parse_qs(urlparse.urlparse(redirect).query)
assert query['state'][0] == state, (query['state'][0], state)
code = query['code'][0]
url = ("https://www.linkedin.com/uas/oauth2/accessToken"
"?grant_type=authorization_code"
"&code=%s&redirect_uri=%s&client_id=%s&client_secret=%s" % (
code, api['REDIRECT_URI'], api['CLIENT_ID'],
api['CLIENT_SECRET']))
try:
response = urllib2.urlopen(url).read()
except urllib2.HTTPError, e:
print "!!ERROR!!"
print e
print e.read()
raise CommandError("Unable to retrieve access token")
access_token = json.loads(response)['access_token']
try:
tokens = LinkedInTokens.objects.get()
tokens.access_token = access_token
tokens.authorization_code = code
except LinkedInTokens.DoesNotExist:
tokens = LinkedInTokens(
access_token=access_token,
authorization_code=code)
tokens.save()

View File

@@ -12,3 +12,12 @@ class LinkedIn(models.Model):
user = models.OneToOneField(User, primary_key=True)
has_linkedin_account = models.NullBooleanField(default=None)
emailed_courses = models.TextField(default="[]") # JSON list of course ids
class LinkedInTokens(models.Model):
"""
For storing access token and authorization code after logging in to
LinkedIn.
"""
access_token = models.CharField(max_length=255)
authorization_code = models.CharField(max_length=255)