Browse Source

client for github, methods implemented: get_authorize_url and get_access_token

mk200789 10 years ago
parent
commit
bac4b37d96

+ 41 - 0
hackathon_starter/hackathon/scripts/github.py

@@ -5,6 +5,7 @@ with Github data and returning the responses as JSON.
 
 import requests
 import simplejson as json
+import urllib, urllib2, urlparse
 
 ########################
 # GITHUB API CONSTANTS #
@@ -12,6 +13,46 @@ import simplejson as json
 
 API_BASE_URL = 'https://api.github.com/users/DrkSephy'
 
+AUTHORIZE_URL = 'https://github.com/login/oauth/authorize'
+ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token'
+
+class GithubOauthClient(object):
+    
+    access_token = None
+    token_type = None
+
+    def __init__(self, client_id, client_secret):
+        self.client_id = client_id
+        self.client_secret = client_secret
+
+    def get_authorize_url(self):
+        auth_setting = {'client_id': self.client_id,
+                        'redirect_uri': 'http://127.0.0.1:8000/hackathon/',
+                        'scope': 'user, public_repo, repo, repo_deployment, notifications, gist'}
+        params = urllib.urlencode(auth_setting)
+        auth_link = AUTHORIZE_URL + '?' + params
+        print auth_link
+        
+        return auth_link
+
+    def get_access_token(self, code):
+        settings = {'client_id': self.client_id,
+                    'client_secret': self.client_secret,
+                    'code': code,
+                    'redirect_uri': 'http://127.0.0.1:8000/hackathon/'}
+        params = urllib.urlencode(settings)
+        access_link = ACCESS_TOKEN_URL + '?' + params
+        req = requests.get(access_link)
+
+        if int(req.status_code) != 200:
+            raise Exception('Invalid response %s' %req.status_code)
+
+        content = urlparse.parse_qs(req.content)
+        self.access_token = content['access_token'][0]
+        self.token_type = content['token_type'][0]
+        print self.access_token
+
+
 def getUserData(clientID, clientSecret):
     '''
     Returns data found on a Github User's public profile.

+ 1 - 0
hackathon_starter/hackathon/urls.py

@@ -26,6 +26,7 @@ urlpatterns = patterns('',
     url(r'^twitterTweets/$', views.twitterTweets, name='twitterTweets'),
     url(r'^tumblr_login/$', views.tumblr_login, name='tumblr_login'),
     url(r'^twitter_login/$', views.twitter_login, name='twitter_login'),
+    url(r'^github_login/$', views.github_login, name='github_login'),
     url(r'^facebook/$', views.facebook, name='facebook'),
     url(r'^quandlSnp500/$', views.quandlSnp500, name='quandlsnp500'),
     url(r'^quandlNasdaq/$', views.quandlNasdaq, name='quandlnasdaq'),

+ 14 - 2
hackathon_starter/hackathon/views.py

@@ -37,17 +37,23 @@ from hackathon.serializers import SnippetSerializer
 from hackathon.forms import UserForm
 
 
+profile_track = None
 getTumblr = TumblrOauthClient(settings.TUMBLR_CONSUMER_KEY, settings.TUMBLR_CONSUMER_SECRET)
 getInstagram = InstagramOauthClient(settings.INSTAGRAM_CLIENT_ID, settings.INSTAGRAM_CLIENT_SECRET)
 getTwitter = TwitterOauthClient(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET, settings.TWITTER_ACCESS_TOKEN, settings.TWITTER_ACCESS_TOKEN_SECRET)
 getLinkedIn= LinkedInAPI(settings.LINKEDIN_API_KEY,settings.LINKEDIN_SECRET_KEY,settings.LINKEDIN_USER_TOKEN,settings.LINKEDIN_USER_SECRET)
+getGithub = GithubOauthClient('2a11ce63ea7952d21f02', '7e20f82a34698fb33fc837186e96b12aaca2618d')
 
 def index(request):
     print "index: " + str(request.user)
 
     if not request.user.is_active:
         if request.GET.items():
-            if 'oauth_verifier' in request.GET.keys():
+            if profile_track == 'github':
+                code = request.GET['code']
+                getGithub.get_access_token(code)
+                print getGithub.access_token
+            elif 'oauth_verifier' in request.GET.keys():
                 oauth_verifier = request.GET['oauth_verifier']
                 getTwitter.get_access_token_url(oauth_verifier) 
 
@@ -530,4 +536,10 @@ def tumblr_login(request):
 
 def twitter_login(request):
     twitter_url = getTwitter.get_authorize_url()     
-    return HttpResponseRedirect(twitter_url)
+    return HttpResponseRedirect(twitter_url)
+
+def github_login(request):
+    github_url = getGithub.get_authorize_url()
+    global profile_track
+    profile_track = 'github'
+    return HttpResponseRedirect(github_url)