You are here

function fb_admin_long_lived_token in Drupal for Facebook 7.4

1 call to fb_admin_long_lived_token()
fb_admin_replace_token_form_submit in ./fb.admin.inc

File

./fb.admin.inc, line 1678

Code

function fb_admin_long_lived_token($token, $app_id = NULL) {

  // @todo determine app id if not passed in.
  // @todo be smart about page tokens, which cannot be converted.
  // Convert short-lived token to long-lived.
  $all_apps = fb_admin_all_apps();
  $app = $all_apps[$app_id];
  if ($app && $app['secret']) {
    try {
      $result = fb_graph('oauth/access_token', array(
        'client_id' => $app['fba'],
        'client_secret' => $app['secret'],
        'grant_type' => 'fb_exchange_token',
        'fb_exchange_token' => $token,
      ), FALSE);
      if (!empty($result['access_token'])) {
        drupal_set_message(t('Using longer-lived token which is set to expire in %duration.', array(
          '%token' => $result['access_token'],
          '%duration' => !empty($result['expires']) ? format_interval($result['expires']) : t('(could not determine expiration)'),
        )));
        return $result['access_token'];
      }
    } catch (Exception $e) {

      // This is reached whenever token belongs to an account instead of a user.  So we don't need to be verbose about it.
      drupal_set_message(t('Could not convert the token into a longer-lived token.  This is expected when token belongs to a page rather than a user.'));
    }
  }
}