Skip to content
Browse files

first commit

  • Loading branch information...
0 parents commit a489eb6ac38bfdc18c09630d2a5a35245cb03cfa @digvan committed Apr 22, 2010
Showing with 92 additions and 0 deletions.
  1. +5 −0 README
  2. 0 __init__.py
  3. +20 −0 models.py
  4. +8 −0 urls.py
  5. +59 −0 views.py
5 README
@@ -0,0 +1,5 @@
+the code is very straight forward
+you can install app easily by adding 'facebook' to your settings.py installed app
+and then run manage.py syncdb
+
+Let me know if you have any question @digvan
0 __init__.py
No changes.
20 models.py
@@ -0,0 +1,20 @@
+from django.db import models
+from django.core.exceptions import *
+#import logging
+
+#LOG_FILENAME = 'facebook.log'
+#logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,format='%(asctime)s %(levelname)s %(message)s')
+#LOGGER = logging.getLogger('DBLog')
+
+class FacebookUser(models.Model):
+ facebook_id = models.CharField(max_length=150,unique=True)
+ created = models.DateTimeField(auto_now_add=True)
+ updated = models.DateTimeField(blank=True,null=True)
+ name = models.CharField(max_length=150)
+ profile_url = models.CharField(max_length=350,blank=True,null=True)
+ access_token = models.CharField(max_length=150,blank=True,null=True)
+ key = models.CharField(max_length=150,blank=True,null=True)
+ secret = models.CharField(max_length=150,blank=True,null=True)
+ location = models.CharField(max_length=150,blank=True,null=True)
+ description = models.CharField(max_length=150,blank=True,null=True)
+ username = models.CharField(max_length=150,blank=True,null=True)
8 urls.py
@@ -0,0 +1,8 @@
+from django.conf.urls.defaults import *
+from kimz.facebook.views import login,logout,home
+
+urlpatterns = patterns('',
+ url(r'^login/?$', view=login,name='login'),
+ url(r'^logout/?$', view=logout,name='logout'),
+ url(r'^$', view=home,name='home'),
+)
59 views.py
@@ -0,0 +1,59 @@
+from django.shortcuts import render_to_response
+from django.http import HttpResponseRedirect,HttpResponse
+from django.core.urlresolvers import reverse
+from django.template import RequestContext
+from django.core.exceptions import *
+from kimz.facebook.models import *
+import time,cgi,urllib
+import simplejson as json
+
+APP_ID = '113380022027046'
+APP_SECRET = '7e0756ac9e65d069cf41b20a4ab965ea'
+
+def login(request):
+ try:
+ verification_code = request.GET.get("code",None)
+ args = dict(client_id=APP_ID, callback="http://digvan.org" + request.path)
+ if request.GET.get("code",None) != None:
+ args["client_secret"] = APP_SECRET
+ args["code"] = request.GET.get("code")
+ response = cgi.parse_qs(urllib.urlopen(
+ "https://graph.facebook.com/oauth/access_token?" +
+ urllib.urlencode(args)).read())
+ access_token = response["access_token"][-1]
+ request.session['access_token'] = access_token
+ me_json = urllib.urlopen("https://graph.facebook.com/me?" + urllib.urlencode(dict(access_token=access_token)))
+ profile = json.load(me_json)
+ user = FacebookUser(facebook_id=str(profile["id"]),name=profile["name"], access_token=access_token)
+ user.save()
+ request.session['fb_user'] = str(profile["id"])
+ return HttpResponseRedirect(reverse('home'))
+ else:
+ return HttpResponseRedirect("https://graph.facebook.com/oauth/authorize?" + urllib.urlencode(args))
+ except Exception,e:
+ #LOGGER.error('Error in login ' + str(e))
+ return render_to_response("facebook.html",{"error":"error in login " + str(e)})
+
+def logout(request):
+ request.session.clear()
+ return render_to_response('facebook.html')
+
+def home(request):
+ try:
+ access_token = request.session.get('access_token',None)
+ fb_user = request.session.get('fb_user',None)
+ user_id = request.GET.get('user',None)
+ if access_token and fb_user:
+ if user_id == None:
+ user = FacebookUser.objects.get(facebook_id__iexact=fb_user)
+ else:
+ user_json = urllib.urlopen("https://graph.facebook.com/" + user_id +"?" + urllib.urlencode(dict(access_token=access_token)))
+ profile = json.load(user_json)
+ user = FacebookUser(facebook_id=str(profile["id"]),name=profile["name"], access_token=access_token,profile_url=profile["profile_url"])
+ user.save()
+ return render_to_response('facebook.html',{'user': user})
+ else:
+ return render_to_response('facebook.html')
+ except Exception,e:
+ #LOGGER.error('Error in home ' + str(e))
+ return render_to_response("facebook.html",{'error':"error in home " + str(e)})

0 comments on commit a489eb6

Please sign in to comment.
Something went wrong with that request. Please try again.