Hello,

Been wondering how easy would it be to perform a graph query in the background but not as an action callback, just straight sphagetti code. For example currently I use Drupal for Facebook to get user's friends like this, would I need to write an action for this? :

	if(user_is_logged_in())
		{
//init app
		$fb_app = fb_get_app(array('label' => "myapp"));
		$_fb = fb_connect_app_init($fb_app);
// check if we have access token
		try {
			$me = $_fb->api('/me');
		} catch (FacebookApiException $e) {
			drupal_set_message("Your Facebook login has expired, please click on the blue Facebook Connect button at the top.");
		}

		if ($me)
			{
			//$fbu = $_fb->getUser();
			//dpm($fbu);
			dpm($_fb->api('/me/friends'));
			}
		}

Comments

quicksketch’s picture

The FBOAuth module does not currently store the $access_token anywhere that is accessible outside of requesting the user to execute an action. In order to attempt to execute an action without the user's explicit request of that action, you would need to record the $access_token returned from Facebook somewhere (like in the $_SESSION variable) during a previous action's processing.

For the purposes of this module, what you're asking to do is not really supported. If you're wanting to do something like import a user's friends upon login, you'd need to make a different action for something like "login_and_import_friends", which would log the user in (just by calling the existing login callback) but then store the access token elsewhere to start doing the import after login.

giorgio79’s picture

Status: Active » Closed (works as designed)

Much appreciated quicksketch, then I will write actions as this module seems very stable.

giorgio79’s picture

Status: Closed (works as designed) » Active

Just tired to write a simple action, but these seem to be hardcoded for display.

For example, here is an action I just tried putting together for getting friends:

/**
 * Implements hook_fboauth_actions().
 */
function mymodule_fboauth_actions() {
  // Give each action a unique key, such as "mymodule_photo_import" for a photo
  // import. This function should begin with the name of your module.
  $actions['mymodule_get_friends'] = array(
    // Give you action a human-readable name. This will be used when showing
    // the user a link to start this action.
    'title' => t('Get Friends'),

    // Specify the name of your callback function that contains the import.
    'callback' => 'mymodule_fboauth_action_get_friends',
  );
  return $actions;
}	

/**
 * Facebook OAuth action callback; 
 */
function mymodule_fboauth_action_get_friends($app_id, $access_token) {
  // Query against the Facebook Graph API. See the Facebook API for a list of
  // commands: http://developers.facebook.com/docs/reference/api/
  $result = fboauth_graph_query('me/friends', $access_token);

  // Optionally return a path to which the user will be redirected. If not set
  // the path in the $_REQUEST['destination'] variable will be used. If there
  // is no path at all specified, the user will be redirected to the homepage.
  return 'mymodule/import-complete';
}

I don't see how I could just get the list of ids to my function, as this is only returning a path. Any tips? :)

quicksketch’s picture

I don't see how I could just get the list of ids to my function, as this is only returning a path. Any tips? :)

The intention is that you would call your function from within your callback (or just put the necessary code directly in the callback). If you're planning on doing this "in the background", you could also make an entry in a database table and do the request on a frequently running cron job (along with the $access_token that you would need.

giorgio79’s picture

Thanks quicksketch.

Currently I use the Drupal menu system for my custom links, so I can hook into Drupal access control and custom page callback.

Here is the flow:
1. User clicks on a custom link
2. With Drupal hook menu I check if user has access, in this check I verify that access token is valid etc.

function mymodule_menu() {
  $items = array();

  $items['play'] = array(
    'title' => 'Play',
    'page callback' => 'mymodule_play',
    'access callback' => 'mymodule_veriy_access',
    'type' => MENU_CALLBACK
  );
  return $items;
}

3. With Drupal hook menu I specify a custom page_callback to perform custom code
4. Use FB oauth in page callback to get list of friends
5. Redirect the user to a node given the result

It seems like I would need to rewrite the entire logic around fboauth action links if I wish to access the graph api but I prefer to stick with Drupal menu system for now and just write some custom code.
Thanks for your help anyway.

quicksketch’s picture

3. With Drupal hook menu I specify a custom page_callback to perform custom code
4. Use FB oauth in page callback to get list of friends

Sounds similar to what was requested a few days ago, see: #1420710: Initiating an action when user browses to a page

giorgio79’s picture

Status: Active » Closed (works as designed)

Much appreciated quicksketch, I will keep on playing with it and adjusting my frame of mind. :)

giorgio79’s picture

Issue summary: View changes

clarify

skh’s picture

Issue summary: View changes

Sorry wrong advice.