浏览代码

Added sign in with google. Also, added an API example to get user basic info, fixed a typo on facebook.py

Liron Shimrony 10 年之前
父节点
当前提交
64d90316da

+ 7 - 1
hackathon_starter/hackathon/models.py

@@ -74,4 +74,10 @@ class FacebookProfile(models.Model):
     time_created = models.DateTimeField(auto_now_add=True)
     profile_url = models.CharField(max_length=50)
     access_token = models.CharField(max_length=100)
-    
+    
+class GoogleProfile(models.Model):
+    user = models.ForeignKey(User)
+    google_user_id = models.CharField(max_length=100)
+    time_created = models.DateTimeField(auto_now_add=True)
+    access_token = models.CharField(max_length=100)
+    profile_url = models.CharField(max_length=100)

+ 1 - 1
hackathon_starter/hackathon/scripts/facebook.py

@@ -95,7 +95,7 @@ class FacebookOauthClient(object):
 		via Facebook.
 
 		Returns:
-			content: dicionay
+			content: dictionary
 				-A dictionary containing user likes.
 		'''
 		#Check if permission exists or ask for it

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

@@ -18,6 +18,7 @@
 		<div class="col-sm-4"><a href="http://localhost:8000/hackathon/yelp/">Yelp</a></div>
 		<div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/nytimesarticles/">New York Times</a></div>
         <div class="col-sm-4"><a href="http://localhost:8000/hackathon/facebook/">Facebook Get User Info Exmaple</a></div>
+        <div class="col-sm-4"><a href="http://localhost:8000/hackathon/google/">Google Get User Info Exmaple</a></div>
 
   	</div>
 

+ 4 - 0
hackathon_starter/hackathon/templates/hackathon/login.html

@@ -50,6 +50,10 @@
           <i class="fa fa-facebook"></i>
           Sign in with Facebook
         </a> 
+        <a class="btn btn-block btn-social btn-google" href="http://localhost:8000/hackathon/google_login/">
+          <i class="fa fa-google"></i>
+          Sign in with Google+
+        </a> 
       </div>
     </body>
 </html>

+ 2 - 0
hackathon_starter/hackathon/urls.py

@@ -30,6 +30,8 @@ urlpatterns = patterns('',
     url(r'^linkedin_login/$', views.linkedin_login, name='linkedin_login'),
     url(r'^facebook_login/$', views.facebook_login, name='facebook_login'),
     url(r'^facebook/$', views.facebook, name='facebook'),
+    url(r'^google_login/$', views.google_login, name='google_login'),
+    url(r'^google/$', views.googlePlus, name='googlePlus'),
     url(r'^quandlSnp500/$', views.quandlSnp500, name='quandlsnp500'),
     url(r'^quandlNasdaq/$', views.quandlNasdaq, name='quandlnasdaq'),
     url(r'^quandlNasdaqdiff/$', views.quandlNasdaqdiff, name='quandlnasdaqdiff'),

+ 45 - 2
hackathon_starter/hackathon/views.py

@@ -12,7 +12,7 @@ from django.views.decorators.csrf import csrf_exempt
 from django.http import JsonResponse
 
 import requests
-import pdb
+
 
 # Scripts
 from scripts.steam import gamespulling, steamidpulling 
@@ -28,6 +28,7 @@ from scripts.meetup import *
 from scripts.linkedin import LinkedinOauthClient
 from scripts.yelp import requestData
 from scripts.facebook import *
+from scripts.googlePlus import *
 
 # Python
 import oauth2 as oauth
@@ -36,7 +37,7 @@ from rest_framework.renderers import JSONRenderer
 from rest_framework.parsers import JSONParser
 
 # Models
-from hackathon.models import Snippet, Profile, InstagramProfile, TwitterProfile, MeetupToken, GithubProfile, LinkedinProfile, FacebookProfile
+from hackathon.models import Snippet, Profile, InstagramProfile, TwitterProfile, MeetupToken, GithubProfile, LinkedinProfile, FacebookProfile, GoogleProfile
 from hackathon.serializers import SnippetSerializer
 from hackathon.forms import UserForm
 
@@ -48,6 +49,7 @@ getTwitter = TwitterOauthClient(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_
 getGithub = GithubOauthClient('2a11ce63ea7952d21f02', '7e20f82a34698fb33fc837186e96b12aaca2618d')
 getLinkedIn = LinkedinOauthClient(settings.LINKEDIN_CLIENT_ID, settings.LINKEDIN_CLIENT_SECRET)
 getFacebook = FacebookOauthClient(settings.FACEBOOK_APP_ID, settings.FACEBOOK_APP_SECRET)
+getGoogle = GooglePlus(settings.GOOGLE_PLUS_APP_ID, settings.GOOGLE_PLUS_APP_SECRET)
 
 def index(request):
     print "index: " + str(request.user)
@@ -148,6 +150,32 @@ def index(request):
                 user = authenticate(username=username+'_facebook', password='password')
                 login(request, user)
 
+            elif profile_track == 'google':
+                code = request.GET['code']
+                state = request.GET['state']
+                getGoogle.get_access_token(code, state)
+                userInfo = getGoogle.get_user_info()
+                username = userInfo['given_name'] + userInfo['family_name']
+
+                try:
+                    user = User.objects.get(username=username+'_google')
+                except User.DoesNotExist:
+                    new_user = User.objects.create_user(username+'_google', username+'@madewithgoogleplus', 'password')
+                    new_user.save()
+
+                    try:
+                        profle = GoogleProfile.objects.get(user = new_user.id)
+                        profile.access_token = getGoogle.access_token
+                    except:
+                        profile = GoogleProfile()
+                        profile.user = new_user
+                        profile.google_user_id = userInfo['id']
+                        profile.access_token = getGoogle.access_token
+                        profile.profile_url = userInfo['link']
+                    profile.save()
+                user = authenticate(username=username+'_google', password='password')
+                login(request, user)
+
 
 
                 
@@ -234,6 +262,14 @@ def facebook(request):
     userInfo = getFacebook.get_user_info()
     return render(request, 'hackathon/facebookAPIExample.html', { 'userInfo' : userInfo})
 
+#################
+#  GOOGLE API  #
+#################
+def googlePlus(request):
+
+    userInfo = getGoogle.get_user_info()
+    return render(request, 'hackathon/googlePlus.html', {'userInfo' : userInfo})
+
 
 #################
 #    YELP API   #
@@ -688,3 +724,10 @@ def facebook_login(request):
     profile_track = 'facebook'
     facebook_url = getFacebook.get_authorize_url()
     return HttpResponseRedirect(facebook_url)
+
+
+def google_login(request):
+    global profile_track
+    profile_track = 'google'
+    google_url = getGoogle.get_authorize_url()
+    return HttpResponseRedirect(google_url)

+ 3 - 0
hackathon_starter/hackathon_starter/settings.py

@@ -147,3 +147,6 @@ QUANDLAPIKEY = 'fANs6ykrCdAxas7zpMz7'
 
 FACEBOOK_APP_ID = '885847011479129'
 FACEBOOK_APP_SECRET = '9a81d5797b87885ef608463086a5c4ac'
+
+GOOGLE_PLUS_APP_ID = '52433353167-5hvsos5pvd2i2dt62trivbqvfk4qc2pv.apps.googleusercontent.com'
+GOOGLE_PLUS_APP_SECRET = 'mv1ZcpHqMF6uX7NRTLDC2jXR'