You are here

function feeds_oauth_callback2 in Feeds OAuth 7

Menu callback to complete authenticating OAuth 2.

1 string reference to 'feeds_oauth_callback2'
feeds_oauth_menu in ./feeds_oauth.module
Implements hook_menu().

File

./feeds_oauth.module, line 225
The module file.

Code

function feeds_oauth_callback2($id) {
  $fetcher = feeds_importer($id)->fetcher;
  $config = $fetcher
    ->getConfig();
  $code = isset($_GET['code']) ? $_GET['code'] : FALSE;
  $token = FALSE;
  $destination = $_SESSION['feeds']['destination'];
  unset($_SESSION['feeds']);

  // TODO: Rewrite this using OAuth2CurlClient.
  if ($code) {
    $query = array(
      'code' => $code,
      'client_id' => $config['consumer_key'],
      'client_secret' => $config['consumer_secret'],
      'redirect_uri' => url('feeds/oauth2/callback/' . $id, array(
        'absolute' => TRUE,
      )),
      'grant_type' => 'authorization_code',
    );
    $response = drupal_http_request($config['access_token_url'], array(
      'method' => 'POST',
      'headers' => array(
        'Content-Type' => 'application/x-www-form-urlencoded',
      ),
      'data' => http_build_query($query, '', '&'),
    ));
    if ($response->code == 200) {
      $token = drupal_json_decode($response->data);
      if (empty($token)) {
        parse_str($response->data, $token);
      }
    }
    else {
      drupal_set_message(t('Attempt to retrieve access token failed: %response', array(
        '%response' => print_r($response, TRUE),
      )), 'error');
    }
  }
  else {
    drupal_set_message(t('OAuth 2 callback did not receive %code argument.', array(
      '%code' => 'code',
    )), 'error');
  }
  if ($token) {
    global $user;
    _feeds_oauth_store_token($token, $user->uid, $config['site_id']);
  }
  drupal_goto($destination);
}