You are here

function _fb_stream_validate_token in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 6.3 fb_stream.admin.inc \_fb_stream_validate_token()
1 string reference to '_fb_stream_validate_token'
fb_stream_admin_settings in ./fb_stream.admin.inc
Form callback for general settings.

File

./fb_stream.admin.inc, line 193

Code

function _fb_stream_validate_token($form, &$form_state) {
  $values = $form_state['values'];
  $token = $values['fb_stream_token'];
  if (empty($token)) {
    $token = $values['fb_stream_token_select'];
    form_set_value($form['fb_stream_token'], $token, $form_state);
    form_set_value($form['fb_stream_token_select'], NULL, $form_state);
  }
  if (empty($token)) {
    form_set_error('fb_stream_token', t('You must select or paste an access token.'));
    return;
  }
  try {
    $via = fb_graph('app', array(
      'access_token' => $token,
    ));
    if ($values['fb_stream_token_prefer_long']) {
      $app = fb_get_app(array(
        'id' => $via['id'],
      ));
      if ($app && $app->secret) {
        try {
          $result = fb_graph('oauth/access_token', array(
            'client_id' => $app->id,
            'client_secret' => $app->secret,
            'grant_type' => 'fb_exchange_token',
            'fb_exchange_token' => $token,
          ));
          if (!empty($result['access_token'])) {
            drupal_set_message(t('Using long-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)'),
            )));
            if ($result['access_token'] != $token) {

              // Change submitted value to the longer-lived version.
              form_set_value($form['fb_stream_token'], $result['access_token'], $form_state);
            }
          }
        } 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.'));

          //fb_log_exception($e, t('Failed to convert token to longer-lived token.'));
        }
      }
    }
  } catch (Exception $e) {
    fb_log_exception($e, t('Unable to validate fb_stream token (%token).', array(
      '%token' => $token,
    )));
    form_set_error('fb_stream_token', $e
      ->getMessage());
  }
}