Modelesq
Modelesq

Reputation: 5402

Get facebook friends upon registration

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.

Upvotes: 1

Views: 358

Answers (1)

DMCS
DMCS

Reputation: 31870

Once that user accepts permissions to your app, you can call the /me/friends to get a list of friends, then you can parse thru that list looking at the installed property 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=1

EDIT

PHP example of how to run FQL code. Taken from http://developers.facebook.com/docs/reference/fql/

<?php
  $app_id = 'YOUR_APP_ID';
  $app_secret = 'YOUR_APP_SECRET';
  $my_url = 'POST_AUTH_URL';

  $code = $_REQUEST["code"];

 //auth user
 if(empty($code)) {
    $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id=' 
    . $app_id . '&redirect_uri=' . urlencode($my_url) ;
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }

  //get user access_token
  $token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
    . $app_id . '&redirect_uri=' . urlencode($my_url) 
    . '&client_secret=' . $app_secret 
    . '&code=' . $code;
  $access_token = file_get_contents($token_url);

  // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+uid2+FROM+friend+WHERE+uid1=me()'
    . '&' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  print_r("query results:");
  print_r($fql_query_obj);
  echo '</pre>';

  // Run fql multiquery
  $fql_multiquery_url = 'https://graph.facebook.com/'
    . 'fql?q={"all+friends":"SELECT+uid2+FROM+friend+WHERE+uid1=me()",'
    . '"my+name":"SELECT+name+FROM+user+WHERE+uid=me()"}'
    . '&' . $access_token;
  $fql_multiquery_result = file_get_contents($fql_multiquery_url);
  $fql_multiquery_obj = json_decode($fql_multiquery_result, true);

  //display results of fql multiquery
  echo '<pre>';
  print_r("multi query results:");
  print_r($fql_multiquery_obj);
  echo '</pre>';
?>

Upvotes: 2

Related Questions