浏览代码

views.py handles linkedin login and adding authorized user to linkedin profile, and included simple linkedin example

mk200789 10 年之前
父节点
当前提交
6d69f99135

+ 4 - 2
hackathon_starter/hackathon/scripts/linkedin.py

@@ -21,7 +21,7 @@ class LinkedinOauthClient(object):
                         'client_secret' : self.client_secret,
                         'redirect_uri' : 'http://127.0.0.1:8000/hackathon/',
                         'state' : 'DCEeFWf45A53sdfKef424',
-                        'scope': 'r_basicprofile'}
+                        'scope': 'r_fullprofile'}
 
         params = urllib.urlencode(auth_setting)
         authURL = AUTHORIZATION_URL + '?' + params
@@ -47,7 +47,8 @@ class LinkedinOauthClient(object):
         self.is_authorized = True
 
     def getUserInfo(self):
-        link = 'https://api.linkedin.com/v1/people/~?format=json&oauth2_access_token=' + self.access_token
+        #link = 'https://api.linkedin.com/v1/people/~?format=json&oauth2_access_token=' + self.access_token
+        link = 'https://api.linkedin.com/v1/people/~:(id,first-name,skills,educations,languages,twitter-accounts)?oauth2_access_token='+self.access_token
         headers = {'x-li-format' : 'json',
                    'content-type' : 'application/json'}
         req = requests.get(link, headers=headers)
@@ -57,4 +58,5 @@ class LinkedinOauthClient(object):
             raise Exception('Invalid response %s' %req.status_code)
 
         self.user_id = content['id']
+        print content
         return content

+ 1 - 3
hackathon_starter/hackathon/templates/hackathon/api_examples.html

@@ -7,9 +7,7 @@
 	    <div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/githubUser/">Github Example</a></div>
 	    <div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/steam/">Steam Example</a></div>
 	    <div class="col-sm-4"><a href={{tumblr_url}}>Tumblr Example</a></div>
-	    <!--
-			<div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/linkedin/">LinkedIn Example</a></div>
-		-->
+		<div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/linkedin/">LinkedIn Example</a></div>
 		<div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/twilio/">Twilio Example</a></div>
 		<div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/instagram/">Instagram Example</a></div>
 		<div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/twitter/">Twitter Example</a></div>

+ 39 - 1
hackathon_starter/hackathon/templates/hackathon/linkedin.html

@@ -2,5 +2,43 @@
 <html>
 <body>
 	{% include 'hackathon/base.html' %}
-	<h1 class="text-center"> {{ title }}</h1> 
+	<h1 class="text-center"> {{ title }}</h1>
+	<br>
+	<div class="col-lg-12">
+        <div class="table-responsive">
+            <table class="table table-bordered table-hover table-striped tablesorter">
+                <thead>
+	                <tr>
+	                <th class="header"> First Name <i class="icon-sort"></i></th>
+	                <th class="header"> Skills <i class="icon-sort"></i></th>
+	                <th class="header"> Education <i class="icon-sort"></i></th>
+	                </tr>
+            	</thead>
+            	<tbody>
+	                <tr>
+	                    <td>{{ content.firstName }}</td> 
+	                    <td>
+	                    	{% if content.skills %}
+	                    		{% for i in content.skills.values %}
+	                    			{{i.skill.name}} 
+	                    		{% endfor %}
+	                    	{% else %}
+	                    		None
+	                    	{% endif %}
+	                    </td>
+	                    <td>
+	                    	{% if content.educations %}
+	                    		{% for i in content.educations.values %}
+	                    			<strong>{{i.schoolName }}</strong> [{{i.degree}}, {{i.fieldOfStudy}}] ({{i.startDate.year}} - {{i.endDate.year}})
+	                    		{% endfor %}
+	                    	{% else %}
+	                    		None
+	                    	{% endif %}
+	                    </td>
+	                </tr>
+            	</tbody>
+            </table>
+        </div>
+	</div>
+</body>
 </html>

+ 1 - 1
hackathon_starter/hackathon/templates/hackathon/twitter.html

@@ -3,7 +3,7 @@
 <body>
 	{% include 'hackathon/base.html' %}
 	<h1 class="text-center"> {{ title }}</h1> 
-	</div>
+
 	<div class="col-lg-12">
         <div class="table-responsive">
             <table class="table table-bordered table-hover table-striped tablesorter">

+ 16 - 6
hackathon_starter/hackathon/views.py

@@ -117,8 +117,6 @@ def index(request):
                     profile.save()
                 user = authenticate(username=getLinkedIn.user_id+'_linkedin', password='password')
                 login(request, user)
-
-
     else:
         if request.GET.items():
             user = User.objects.get(username = request.user.username)
@@ -150,6 +148,16 @@ def index(request):
                 except InstagramProfile.DoesNotExist:
                     profile = InstagramProfile(user = user, access_token = getInstagram.access_token, instagram_user=getInstagram.user_data['username'])
                     profile.save()
+            elif profile_track == 'linkedin':
+                code = request.GET['code']
+                getLinkedIn.get_access_token(code)
+                getLinkedIn.getUserInfo()
+
+                try:
+                    linkedinUser = LinkedinProfile.objects.get(user=user.id)
+                except LinkedinProfile.DoesNotExist:
+                    profile = LinkedinProfile(user = user, access_token = getLinkedIn.access_token, linkedin_user=getLinkedIn.user_id)
+                    profile.save()
 
     context = {'hello': 'world'}
     return render(request, 'hackathon/index.html', context)
@@ -452,8 +460,10 @@ def twitter(request):
     if getTwitter.is_authorized:
         value = getTwitter.get_trends_available(settings.YAHOO_CONSUMER_KEY)
     else:
-        tumblr_url = getTwitter.get_authorize_url()
-        return HttpResponseRedirect(tumblr_url)
+        global profile_track
+        profile_track = 'twitter'
+        twitter_url = getTwitter.get_authorize_url()
+        return HttpResponseRedirect(twitter_url)
 
     context ={'title': 'twitter', 'value': value}
     return render(request, 'hackathon/twitter.html', context)
@@ -484,14 +494,14 @@ def twitterTweets(request):
 
 def linkedin(request):
     if getLinkedIn.is_authorized:
-        print 'Authorized'
+        content = getLinkedIn.getUserInfo()
     else:
         global profile_track 
         profile_track = 'linkedin'
         linkedin_url = getLinkedIn.get_authorize_url()
         return HttpResponseRedirect(linkedin_url)
 
-    context = {'title': 'linkedin example'}
+    context = {'title': 'linkedin example', 'content': content}
     return render(request, 'hackathon/linkedin.html', context)