def facebook_connect(request): facebook_app_id = settings.FACEBOOK_APP_ID facebook_secret = settings.FACEBOOK_SECRET args = { "client_id": facebook_app_id, "redirect_uri": "http://" + Site.objects.get_current().domain + reverse('facebook-connect'), "scope": "offline_access,user_website,publish_actions", "display": "popup", } verification_code = request.REQUEST.get('code') if verification_code: args["client_secret"] = facebook_secret args["code"] = verification_code uri = "https://graph.facebook.com/oauth/access_token?" + \ urllib.urlencode(args) response_text = urllib.urlopen(uri).read() response = urlparse.parse_qs(response_text) if "access_token" not in response: logging.user(request, "~BB~FRFailed Facebook connect") return dict(error="Facebook has returned an error. Try connecting again.") access_token = response["access_token"][-1] # Get the user's profile. graph = facebook.GraphAPI(access_token) profile = graph.get_object("me") uid = profile["id"] # Be sure that two people aren't using the same Facebook account. existing_user = MSocialServices.objects.filter(facebook_uid=uid) if existing_user and existing_user[0].user_id != request.user.pk: user = User.objects.get(pk=existing_user[0].user_id) logging.user(request, "~BB~FRFailed FB connect, another user: %s" % user.username) return dict(error=("Another user (%s, %s) has " "already connected with those Facebook credentials." % (user.username, user.email or "no email"))) social_services, _ = MSocialServices.objects.get_or_create(user_id=request.user.pk) social_services.facebook_uid = uid social_services.facebook_access_token = access_token social_services.syncing_facebook = True social_services.save() SyncFacebookFriends.delay(user_id=request.user.pk) logging.user(request, "~BB~FRFinishing Facebook connect") return {} elif request.REQUEST.get('error'): logging.user(request, "~BB~FRFailed Facebook connect") return {'error': '%s... Try connecting again.' % request.REQUEST.get('error')} else: # Start the OAuth process logging.user(request, "~BB~FRStarting Facebook connect") url = "https://www.facebook.com/dialog/oauth?" + urllib.urlencode(args) return {'next': url}
def facebook_connect(request): facebook_app_id = settings.FACEBOOK_APP_ID facebook_secret = settings.FACEBOOK_SECRET args = { "client_id": facebook_app_id, "redirect_uri": "http://" + Site.objects.get_current().domain + reverse('facebook-connect'), "scope": "offline_access,user_website,publish_actions", "display": "popup", } verification_code = request.REQUEST.get('code') if verification_code: args["client_secret"] = facebook_secret args["code"] = verification_code uri = "https://graph.facebook.com/oauth/access_token?" + \ urllib.urlencode(args) response_text = urllib.urlopen(uri).read() response = urlparse.parse_qs(response_text) if "access_token" not in response: logging.user(request, "~BB~FRFailed Facebook connect") return dict(error="Facebook has returned an error. Try connecting again.") access_token = response["access_token"][-1] # Get the user's profile. graph = facebook.GraphAPI(access_token) profile = graph.get_object("me") uid = profile["id"] # Be sure that two people aren't using the same Facebook account. existing_user = MSocialServices.objects.filter(facebook_uid=uid) if existing_user and existing_user[0].user_id != request.user.pk: user = User.objects.get(pk=existing_user[0].user_id) logging.user(request, "~BB~FRFailed FB connect, another user: %s" % user.username) return dict(error=("Another user (%s, %s) has " "already connected with those Facebook credentials." % (user.username, user.email or "no email"))) social_services, _ = MSocialServices.objects.get_or_create(user_id=request.user.pk) social_services.facebook_uid = uid social_services.facebook_access_token = access_token social_services.syncing_facebook = True social_services.save() SyncFacebookFriends.delay(user_id=request.user.pk) logging.user(request, "~BB~FRFinishing Facebook connect") return {} elif request.REQUEST.get('error'): logging.user(request, "~BB~FRFailed Facebook connect") return {'error': '%s... Try connecting again.' % request.REQUEST.get('error')} else: # Start the OAuth process logging.user(request, "~BB~FRStarting Facebook connect") url = "https://www.facebook.com/dialog/oauth?" + urllib.urlencode(args) return {'next': url}