Get friends using FQL with an application access token

Ben Sion picture Ben Sion · Mar 16, 2012 · Viewed 10.7k times · Source

I am trying to use the Facebook Query Language (FQL) to get a list of friends, who have installed my app. After authenticating my app and getting my app access token, I am executing the following query (note I am using Java and RestFB to run):

https://api-read.facebook.com/method/fql.query?query=SELECT+uid+FROM+user+WHERE+uid+IN+%28SELECT+uid2+FROM+friend+WHERE+uid1%3D<userId>%29+AND+is_app_user%3D1&access_token=<appAccessToken>&format=json

this returns an error:

..."error_code":102,"error_msg":"Requires user session"

Similarly if I try using FQL with the graph API (like below) I get the same error.

https://graph.facebook.com/fql?q=SELECT+uid+FROM+user+WHERE+uid+IN+%28SELECT+uid2+FROM+friend+WHERE+uid1%3D<userId>%29+AND+is_app_user%3D1&access_token=<appAccessToken>&format=json

So I tried using the user access token an as expected it works. I have searched a fair bit and came across this: Facebook 'Friends.getAppUsers' using Graph API

which has some great advice and led me to try the following query:

https://graph.facebook.com/<userId>/friends?fields=installed&access_token=<appAccessToken>&format=json

And this returns the list of friends with the app access token.

Which leads me to my question. Why am I unable to use the app access token with FQL to get a list of friends but I can retrieve the friends with the Graph API?

Thanks in advance,

Ben

Answer

Vijay picture Vijay · Mar 16, 2012

First i would like to bring into your notice that fql.query is from REST API and its going to be deprecated.

FB recommends to use GRAPH API going forward..

Important note: FQL is not going to be deprecated but only the fql.query method using REST API is going to be deprecated.

So you can continue to use FQL and execute the FQL using GRAPH API.

Read more in http://developers.facebook.com/docs/reference/fql/

Example code in FB

 //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>';

I know that i haven't actually answered your question, but this is just a collection of information about which can used going forward to be inline with FB changes.