When a user registers for my site through Facebook. It returns “3 friends have registered” next to the ‘Register’ button with the Facebook authentication API.
def facebook_authenticated(request):
try:
code = request.GET['code']
except:
return HttpResponseRedirect('/?error=facebook')
redirect_url = "yaddayadaa"
args = dict(client_id=settings.FACEBOOK_API_ID, redirect_uri=redirect_url)
args["client_secret"] = settings.FACEBOOK_API_SECRET
args["code"] = code
response = cgi.parse_qs(urllib.urlopen(
"https://graph.facebook.com/oauth/access_token?" +
urllib.urlencode(args)).read())
access_token = response["access_token"][-1]
graph_url = "https://graph.facebook.com/me?access_token="+access_token
profile = json.load(urllib.urlopen(graph_url))
member_id = str(profile['id'])
try:
new_user = FacebookProfile.objects.get(facebook_id=member_id).user
fbprofile = FacebookProfile.objects.get(user=new_user)
if access_token != fbprofile.access_token:
fbprofile.access_token = access_token
fbprofile.save()
except (User.DoesNotExist, ObjectDoesNotExist):
return HttpResponseRedirect('/?error=facebook')
How would I get the 3 users and store, such that the registering user can connect with them on my site?
I haven’t found any examples of how to accomplish this, or what parts of the API are useful to do this.
Once that user accepts permissions to your app, you can call the
/me/friendsto get a list of friends, then you can parse thru that list looking at theinstalledproperty to determine if that friend is one of your app users.Another way, the better of the two as only one call as well as no parsing after the call, would be to use FQL to query the friends list
fql?q=SELECT uid, name, is_app_user FROM user WHERE uid IN (SELECT uid1 FROM friend WHERE uid2=me()) and is_app_user=1EDIT
PHP example of how to run FQL code. Taken from http://developers.facebook.com/docs/reference/fql/