Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

url for foursquare, view and backend for authenticating foursquare users

  • Loading branch information...
commit 0cfe12a0cb18d1b2d9cfbbac7b6164fb5e25a392 1 parent 87e8987
@akshar-raaj authored
View
23 socialauth/auth_backends.py
@@ -7,7 +7,7 @@
import urllib
from socialauth.lib import oauthtwitter2 as oauthtwitter
from socialauth.models import OpenidProfile as UserAssociation, \
-TwitterUserProfile, FacebookUserProfile, LinkedInUserProfile, AuthMeta, GithubUserProfile
+TwitterUserProfile, FacebookUserProfile, LinkedInUserProfile, AuthMeta, GithubUserProfile, FoursquareUserProfile
from socialauth.lib.linkedin import *
import random
@@ -349,3 +349,24 @@ def get_user(self, user_id):
return User.objects.get(pk=user_id)
except:
return None
+
+class FoursquareBackend:
+ def authenticate(self, foursquare_access_token):
+ try:
+ foursquare_profile = FoursquareUserProfile.objects.get(access_token=foursquare_access_token)
+ return foursquare_profile.user
+ except FoursquareUserProfile.DoesNotExist:
+ foursquare_user_count = FoursquareUserProfile.objects.all().count()
+ username = "FoursquareUser:" + str(foursquare_user_count+1)
+ user = User(username=username)
+ user.save()
+ foursquare_user = FoursquareUserProfile(user=user, access_token=foursquare_access_token)
+ foursquare_user.save()
+ AuthMeta(user=user, provider='Foursquare').save()
+ return foursquare_user.user
+
+ def get_user(self, user_id):
+ try:
+ return User.objects.get(pk=user_id)
+ except:
+ return None
View
2  socialauth/urls.py
@@ -23,6 +23,8 @@
url(r'^openid/done/$', 'openid_done', name='openid_openid_done'),
url(r'^github_login/$', 'github_login', name='github_login'),
url(r'github_login/done/$', 'github_login_done', name='github_login_done'),
+ url(r'^foursquare_login/$', 'foursquare_login', name='foursquare_login'),
+ url(r'^foursquare_login/done/$', 'foursquare_login_done', name='foursquare_login_done'),
)
#Other views.
View
26 socialauth/views.py
@@ -19,6 +19,7 @@
from socialauth.lib.linkedin import *
from socialauth.lib.github import GithubClient
+from socialauth.lib import foursquare
LINKEDIN_CONSUMER_KEY = getattr(settings, 'LINKEDIN_CONSUMER_KEY', '')
LINKEDIN_CONSUMER_SECRET = getattr(settings, 'LINKEDIN_CONSUMER_SECRET', '')
@@ -333,3 +334,28 @@ def github_login_done(request):
login(request, user)
return HttpResponseRedirect(LOGIN_REDIRECT_URL)
return HttpResponseRedirect(LOGIN_URL)
+
+def foursquare_login(request):
+ foursquare_client = foursquare.FourSquareClient()
+ return HttpResponseRedirect(foursquare_client.get_authentication_url())
+
+def foursquare_login_done(request):
+ try:
+ code = request.GET.get('code')
+ except:
+ """Some error ocurred.
+ Rediect to login page"""
+ return HttpResponseRedirect(LOGIN_URL)
+ request.session['foursquare_code'] = code
+ foursquare_client = foursquare.FourSquareClient()
+ access_token_response = foursquare_client.get_access_token(request.session['foursquare_code'])
+ import json
+ access_token = json.loads(access_token_response)['access_token']
+ try:
+ user = authenticate(foursquare_access_token=access_token)
+ except:
+ user=None
+ if user:
+ login(request, user)
+ return HttpResponseRedirect(LOGIN_REDIRECT_URL)
+ return HttpResponseRedirect(LOGIN_URL)
Please sign in to comment.
Something went wrong with that request. Please try again.