def authenticate(self, token=None, request=None): args = { 'client_id': settings.FACEBOOK_APP_ID, 'client_secret': settings.FACEBOOK_APP_SECRET, 'redirect_uri': request.build_absolute_uri(reverse('facebook_oauth.views.authenticate_view')), 'code': token, } target = urllib.urlopen('https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args)).read() response = cgi.parse_qs(target) access_token = response['access_token'][-1] user_json = urllib.urlopen('https://graph.facebook.com/me?' + urllib.urlencode(dict(access_token=access_token))) fb_profile = simplejson.load(user_json) try: fb_user = FacebookUser.objects.get(facebook_id=str(fb_profile['id'])) except FacebookUser.DoesNotExist: if request.user.is_authenticated(): fb_user = FacebookUser(user=request.user, facebook_id=fb_profile['id']) else: request.session['fb_profile'] = fb_profile return None fb_user.access_token = access_token fb_user.save() return fb_user.user
def register_view(request): try: fb_profile = request.session['fb_profile'] except KeyError: return HttpResponseRedirect('/') if request.method == 'POST': user = User.objects.create_user(request.POST['username'], request.POST['email']) fb_user = FacebookUser(user=user, facebook_id=fb_profile['id']) fb_user.save() del request.session['fb_profile'] return HttpResponseRedirect(reverse('facebook_oauth.views.authenticate_view') + '?ignorereferer=1') else: return render_to_response('member/register-facebook.html', context_instance=RequestContext(request))
def register_view(request): try: fb_profile = request.session["fb_profile"] except KeyError: return HttpResponseRedirect("/") if request.method == "POST": user = User.objects.create_user(request.POST["username"], request.POST["email"]) fb_user = FacebookUser(user=user, facebook_id=fb_profile["id"]) fb_user.save() del request.session["fb_profile"] return HttpResponseRedirect(reverse("facebook_oauth.views.authenticate_view") + "?ignorereferer=1") else: return render_to_response("member/register-facebook.html", context_instance=RequestContext(request))
def authenticate(self, token=None, request=None): args = { 'client_id': settings.FACEBOOK_APP_ID, 'client_secret': settings.FACEBOOK_APP_SECRET, 'redirect_uri': request.build_absolute_uri( reverse('facebook_oauth.views.authenticate_view')), 'code': token, } target = urllib.urlopen( 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args)).read() response = cgi.parse_qs(target) access_token = response['access_token'][-1] profile_json = urllib.urlopen( 'https://graph.facebook.com/me?' + urllib.urlencode(dict(access_token=access_token))) fb_profile = simplejson.load(profile_json) try: fb_user = FacebookUser.objects.get( facebook_id=str(fb_profile['id'])) except FacebookUser.DoesNotExist: fb_user = FacebookUser(facebook_id=str(fb_profile['id'])) fb_user.access_token = access_token fb_user.profile_json = str(profile_json) if not fb_user.user_id: try: fb_user.user = User.objects.get(username='******' % fb_user.facebook_id, ) except User.DoesNotExist: fb_user.user, created = User.objects.get_or_create( username='******' % fb_user.facebook_id, first_name=fb_profile['first_name'], last_name=fb_profile['last_name'], ) fb_user.save() return fb_user.user
def register_view(request): try: fb_profile = request.session['fb_profile'] except KeyError: return HttpResponseRedirect('/') if request.method == 'POST': user = User.objects.create_user(request.POST['username'], request.POST['email']) fb_user = FacebookUser(user=user, facebook_id=fb_profile['id']) fb_user.save() del request.session['fb_profile'] return HttpResponseRedirect( reverse('facebook_oauth.views.authenticate_view') + '?ignorereferer=1') else: return render_to_response('member/register-facebook.html', context_instance=RequestContext(request))
def authenticate(self, token=None, request=None): args = { 'client_id': settings.FACEBOOK_APP_ID, 'client_secret': settings.FACEBOOK_APP_SECRET, 'redirect_uri': request.build_absolute_uri( reverse('facebook_oauth.views.authenticate_view')), 'code': token, } target = urllib.urlopen( 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args)).read() response = cgi.parse_qs(target) access_token = response['access_token'][-1] user_json = urllib.urlopen( 'https://graph.facebook.com/me?' + urllib.urlencode(dict(access_token=access_token))) fb_profile = simplejson.load(user_json) try: fb_user = FacebookUser.objects.get( facebook_id=str(fb_profile['id'])) except FacebookUser.DoesNotExist: if request.user.is_authenticated(): fb_user = FacebookUser(user=request.user, facebook_id=fb_profile['id']) else: request.session['fb_profile'] = fb_profile return None fb_user.access_token = access_token fb_user.save() return fb_user.user
def authenticate(self, token=None, request=None): args = { 'client_id': settings.FACEBOOK_APP_ID, 'client_secret': settings.FACEBOOK_APP_SECRET, 'redirect_uri': request.build_absolute_uri(reverse('facebook_oauth.views.authenticate_view')), 'code': token, } target = urllib.urlopen('https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args)).read() response = cgi.parse_qs(target) access_token = response['access_token'][-1] profile_json = urllib.urlopen('https://graph.facebook.com/me?' + urllib.urlencode(dict(access_token=access_token))) fb_profile = simplejson.load(profile_json) try: fb_user = FacebookUser.objects.get(facebook_id=str(fb_profile['id'])) except FacebookUser.DoesNotExist: fb_user = FacebookUser(facebook_id=str(fb_profile['id'])) fb_user.access_token = access_token fb_user.profile_json = str(profile_json) if not fb_user.user_id: try: fb_user.user = User.objects.get(username='******' % fb_user.facebook_id,) except User.DoesNotExist: fb_user.user, created = User.objects.get_or_create( username='******' % fb_user.facebook_id, first_name=fb_profile['first_name'], last_name=fb_profile['last_name'], ) fb_user.save() return fb_user.user