Sfoglia il codice sorgente

Merge pull request #10 from lsxliron/master

Fixed FB documentation, added google login and added google+ API to readme
David Leonard 10 anni fa
parent
commit
86e15580cd

+ 39 - 6
README.md

@@ -193,17 +193,50 @@ Getting API Keys
 <img src="http://www.freelargeimages.com/wp-content/uploads/2014/11/Facebook_logo-6.jpg" width="200">
 
 1. Register an account on [Facebook.com](http://www.facebook.com.com/)
-2. Visit [Facebook Developer Network page](https://developers.facebook.com/)
-3. After logging in, Click on **My Apps** and then on **Add a New App+**
-    * Choose Website as the platform and add the **name** for your project
-    * Click on **Create New Facebook APP ID** and choose the **Category** of your application
+2. Visit [Facebook Developers page](https://developers.facebook.com/)
+3. After logging in, Click on **My Apps** and then on **Add a New App**
+    * Choose W**ebsite** as the platform and add the **name** for your project
+    * Give your app a name.
+    * Choose the category your app falls into.
     * Click **Create App ID**
-4. After the captcha, scroll down past the quick start and add `http://localhost:8000/`
-5. Within your `views.py` add the **App ID** in `yourappid` underneath the view for your facebook application.
+    * Skip the quickstart process and you will be redirected to the app dashboard.
+4. Copy the **app ID** and the **app secret**.
+5. From the left menu choose the **Settings** option.
+6. Click on **Add Platform** and choose **Website** once again.
+7. Under **site URL**, specift the URL to be redirected after authentication is complete.
+8. Click save.
+9. In ```settings.py``` change the following values:
+    * ```FACEBOOK_APP_ID = your_app_id```
+    * ```FACEBOOK_APP_SECRET = your_app_secret```
 
 
 <hr>
 
+<img src="https://travelblogsuccess.com/wp-content/uploads/2013/09/how-to-use-google-plus-for-business.jpg" width="200" />
+
+1. Register an account on [Google.com](https://accounts.google.com/signup).
+2. Navigate to [Google Developer Console](https://console.developers.google.com/project).
+3. Click on **Create Project**, give your app a name and click **Create** (this might take a few sceonds).
+4. You will be redirected to the project dashboard. From the left menu choose **APIs & auth** and then choose **APIs**.
+5. Choose the API you would like to use (the built in example uses **Google+ API**).
+6. Click on **Enable API**.
+7. From the side menu, under **APIs & auth** select **consent screen**.
+    * Fill your app name under **Product Name**.
+    * Hit **save** button on the bottom.
+8. From the side menu, under **APIs & auth** select credentials:
+    * Click on **Create new Client ID**.
+    * Under **Authorized JavaScript origins** specify you app base address (e.g ```http://localhost:8000```).
+    * Under **Authorized redirect URIs** specify the URL to be redirected after authentication is complete.
+    * Hit **Create Client ID** button (this might also take a few seconds).
+9. Copy your new generated ```client_id``` and ```client_secret```:
+10. Under ```settings.py``` change the following values:
+    * ```GOOGLE_PLUS_APP_ID = your_client_id```
+    * ```GOOGLE_PLUS_APP_SECRET = your_client_secret```
+
+<hr>
+
+
+
 <img src="https://secure.assets.tumblr.com/images/logo_page/img_logotype_34465d_2x.png" width="200">
 
 1. Register an account on Tumblr.com.

+ 7 - 1
hackathon_starter/hackathon/models.py

@@ -83,4 +83,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'),

+ 49 - 4
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, TumblrProfile
+from hackathon.models import Snippet, Profile, InstagramProfile, TwitterProfile, MeetupToken, GithubProfile, LinkedinProfile, FacebookProfile, TumblrProfile, 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)
@@ -152,7 +154,6 @@ def index(request):
                     oauth_verifier = request.GET['oauth_verifier']
                     getTumblr.access_token_url(oauth_verifier) 
                     getTumblr.getUserInfo()
-
                     try:
                         user = User.objects.get(username = getTumblr.username + '_tumblr')
                     except User.DoesNotExist:
@@ -167,7 +168,36 @@ def index(request):
                             profile = TumblrProfile(user=new_user, access_token=getTumblr.access_token['oauth_token'], access_token_secret= getTumblr.access_token['oauth_token_secret'], tumblr_user=getTumblr.username)
                         profile.save()
                 user = authenticate(username=getTumblr.username+'_tumblr', password='password')
-                login(request, user)                
+                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)
+
+
     else:
         if request.GET.items():
             user = User.objects.get(username = request.user.username)
@@ -261,6 +291,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   #
@@ -700,3 +738,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'