OK, so i finally understand that FB Graph is their latest API they are using(am i right?).

Now, what i want to achieve is suppose to be very simple(or so i thought): When a user enters a product page i want to show him all of his FB friends who bought the same product.

So i installed the facebook connect and that part went well.
but now i need to get all of the user's friends so i can find out which of them bought the same product.

i found this great tutorial: Connecting Facebook with Drupal, the easy way: Part 2
but that doesn't help me because it explain that i'll need to preform some kind of action so the user will be directed to facebook and then comeback to a different page...

so i looked in all of this module function and i can't find anything that can bring me the access token without redirecting to facebook and return with the $_REQUEST['code'].
That's of course no good for me because i don't want to do a redirect to faecbook every time someone is entering a product...

so, is that module the right thing for what i want to achieve? i think it suppose to be because it should handle the oauth...

Comments

doronsever’s picture

OK

I am posting what i have found for 2 reasons
1: So people that need the same thing will have a solution
2: If there is a better way, please let me know

So, what i got is when a user confirms my app at facebook after his first login, i can get the info that the user approved with my app's permission.

So if the user approved my app, i need to get the access token of my app

$access_token = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id='.APP_ID.'&client_secret='.APP_SECRET.'&grant_type=client_credentials');

then i can get the users friends by doing so:

$friend_list = drupal_json_decode(file_get_contents('https://graph.facebook.com/[USER FB ID]/friends?access_token=' . $access_token));

so i hope that helps someone, and if there's a better way, please let me know :)

quicksketch’s picture

Hi @doronsever, the approach you've described should work fine, though it's not using most of what FBOAuth provides. At the very least, you can use fboauth_graph_query() instead of file_get_contents(), but the result is the same.

FBOAuth intentionally does not store the access tokens from Facebook, because it assume they will expire. After a user has authorized your app (especially if you've already requested the necessary permissions upon login), using an FBOAuth action (as described in the README.txt file) should be an invisible process for your end users. They won't see a Facebook login screen or permissions request if they're already logged in and your app has permission (both of which will be true if the user logged in through Facebook). You can redirect the user back to the same page or a different one if you like. So even though the user is going to Facebook and back again, the process is invisible. Try logging into your site using FBOAuth and then visiting the user/x/edit profile edit form. Click on the "More info" link next to the Facebook Connect settings and try disconnecting your account from Facebook. That's an example of what a FBOAuth action will look like, and as you should find, the process of going to Facebook and back is invisible.

However if you would like to use the existing token acquired during login rather than retrieving a new one for each action, you can attempt to do so with this sandbox project, which stores the tokens of each users login in your database: http://drupal.org/sandbox/pl2/1600932. Just keep in mind that such tokens are not permanent, and you may encounter a situation where the token you attempt to use is no longer valid and the request will fail. In such a case, you'll need to retrieve a new token by performing a new FBOAuth action (such as a user login through Facebook).

doronsever’s picture

Hi @quicksketch.
First of all thank you for your reply.
My problem was in ajax calls, i wanted to get the user's friend list by ajax and i couldn't do so because i get a 302(because there is a redirect).

I prefer to use FBouth API as i assume it could help me much more in the future but if you can direct me how create an action that will work with an ajax call it would be great!.

Anyway thank you again!

NoSimpin’s picture

Hi @doronsever

I ran into the same problem so here's how I handled it.

I have two menu items with callbacks, modulename_facebook_friends, and modulename_get_facebook_friends and I do an ajax call to modulename_get_facebook_friends to get the json and write to modulename_facebook_friends.

modulename_menu() {
    $items['get-facebook-friends'] = array(
        'title' => 'Example Page',
        'access arguments' => array('access content'), 
        'page callback' => 'modulename_get_facebook_friends',
        'type' => MENU_CALLBACK,
    );
    $items['facebook-friends'] = array(
        'title' => 'Invite Facebook Friends',
        'access arguments' => array('access facebook invite'), 
        'page callback' => 'modulename_facebook_friends',
        'type' => MENU_NORMAL_ITEM,
        'menu_name' => 'main-menu',
    );
    return $items;
  }

To make it work I had to store the access token in a session variable by way of a action callback. So modulename_facebook_friends checks to see if the session variable is set, and if its not it sets it by calling the action programmatically.

function modulename_facebook_friends(){
 if (!isset($_SESSION['access_token'])){
     $fb_link = fboauth_action_link_properties('fboauth_integration_friends_list');
    $fb_query_url = url($fb_link['href'], array('absolute' => TRUE, 'query' => $fb_link['query']));
     drupal_goto($fb_query_url);
 }
$output = "<div id='facebook-friends-container'></div>";
}


function fboauth_integration_get_facebook_friends(){
   $result =  fboauth_graph_query('me/friends?fields=groups,name,picture', $_SESSION['access_token']);
   //do something with $result
   echo json_encode(array("friends" => $output));
  }

function modulename_fboauth_action_friends_import($app_id, $access_token) {
$_SESSION['access_token'] = $access_token;
return "facebook-friends";
}

So basically, you go to site/facebook-friends, and it checks to see if "$_SESSION['access_token']" is set, and if it's not "set it". Then do an AJAX call to site/get-facebook-friends which will use the $_SESSION['access_token'] and return JSON . Not sure if this is the best way to do it but so far its working.

geetotes’s picture

Why not just use the facepile iframe/div/fbxml/thing?

https://developers.facebook.com/docs/reference/plugins/facepile/

You can remove the URL and just show friends who like an app?

(I'm asking because I'm confused by your use case.... what do you mean "friends who bought a product"? How are you keeping track of that?)

dharmendra singh’s picture

#4
when we run this (#4) module it returns some page not found url,,$base_url/fboauth/fboauth_integration_friends_list?code=AQC7NIuuhpGCsfGRYRBGD7z8trDBTmdUpV8283e49O20HGkiQ_xPTEdB8vg3_mPXKPlJ9ZYi50CEEtKKdqzDjLiX59apxuRizIDGr5HYLuv_jEk390PXsqbejGepX_xMdXWD37gpk9wVimycq42_OeBpD6BsUQbcedUG419dC4rKNvuahycrsJb5L2u4qyIJFmvbWVoym-8Ope_6gKjOK6hN#_=_

where we can add this page