You are here

function feeds_oauth_refresh2 in Feeds OAuth 7

Menu callback to refresh access tokens for OAuth 2.

1 call to feeds_oauth_refresh2()
feeds_oauth_get_tokens in ./feeds_oauth.module
Get tokens from database.

File

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

Code

function feeds_oauth_refresh2($refresh_token, $uid, $id) {
  $fetcher = feeds_importer($id)->fetcher;
  $config = $fetcher
    ->getConfig();
  $query = array(
    'client_id' => $config['consumer_key'],
    'client_secret' => $config['consumer_secret'],
    'refresh_token' => $refresh_token,
    'grant_type' => 'refresh_token',
  );
  $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, '', '&'),
  ));
  $token = NULL;
  if ($response->code == 200) {
    $token = drupal_json_decode($response->data);

    // Pass on the old refresh token if we didn't get a new one.
    if (empty($token['refresh_token'])) {
      $token['refresh_token'] = $refresh_token;
    }
    _feeds_oauth_store_token($token, $uid, $config['site_id']);
  }
  else {
    watchdog('feeds_oauth', 'Attempt to refresh token failed: %response', array(
      '%response' => print_r($response, TRUE),
    ), WATCHDOG_ERROR);
  }
  return $token;
}