forked from jdx/django_facebook_oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
30 lines (28 loc) · 1.11 KB
/
backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from django.contrib.sites.models import Site
from django.conf import settings
from facebook.models import *
import urllib, cgi, simplejson
class FacebookBackend:
def authenticate(self, token=None, request=None):
args = dict(client_id=settings.APP_ID, callback="http://" + Site.objects.get(id=settings.SITE_ID).domain + '/authenticate/')
args["client_secret"] = settings.APP_SECRET
args["code"] = token
response = cgi.parse_qs(urllib.urlopen(
"https://graph.facebook.com/oauth/access_token?" +
urllib.urlencode(args)).read())
access_token = response["access_token"][-1]
me_json = urllib.urlopen("https://graph.facebook.com/me?" + urllib.urlencode(dict(access_token=access_token)))
profile = simplejson.load(me_json)
try:
fb_user = FacebookUser.objects.get(facebook_id = str(profile["id"]))
except FacebookUser.DoesNotExist:
request.session['fb_id'] = profile['id']
return None
fb_user.access_token = access_token
fb_user.save()
return fb_user.user
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None