无法使用OAUTH对Twitter API进行身份验证

I am trying to authenticate with Twitter for a custom app using Drupal.

For some reason, even though I am including my OAuth credentials, I can't authenticate with the Twitter API.

Here's my code:

/**
 * Pulls Twitter data for processing.
 */
function sentiment_analyzer_twitter_pull() {

  $url = "https://api.twitter.com/1.1/search/tweets.json?q=%23superbowl&result_type=recent"; // test data used by Twitter

  $oauth_access_token = variable_get('sentiment_analyzer_twitter_token');
  $oauth_access_token_secret = variable_get('sentiment_analyzer_twitter_token_secret');
  $consumer_key = variable_get('sentiment_analyzer_twitter_consumer_key');
  $consumer_secret = variable_get('sentiment_analyzer_twitter_consumer_secret');

  $oauth = array('oauth_consumer_key' => $consumer_key,
    'oauth_nonce' => time(),
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_token' => $oauth_access_token,
    'oauth_timestamp' => time(),
    'oauth_version' => '1.0');

  $base_info = sentiment_analyzer_build_base_string($url, 'GET', $oauth);
  $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
  $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
  $oauth['oauth_signature'] = $oauth_signature;

  // Make requests
  $header = array(sentiment_analyzer_build_authorization_header($oauth), 'Expect:');
  $options = array(CURLOPT_HTTPHEADER => $header,
    //CURLOPT_POSTFIELDS => $postfields,
    CURLOPT_HEADER => false,
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false);

  $feed = curl_init();
  curl_setopt_array($feed, $options);
  $json = curl_exec($feed);
  curl_close($feed);

  $twitter_data = json_decode($json);

  watchdog("twitter data", "<pre>" . print_r($twitter_data, true) . "</pre>");

}

function sentiment_analyzer_build_base_string($base_uri, $method, $params) {
  $r = array();
  ksort($params);
  foreach ($params as $key => $value) {
    $r[] = "$key=" . rawurlencode($value);
  }
  return $method . "&" . rawurlencode($base_uri) . '&' . rawurlencode(implode('&', $r));
}

function sentiment_analyzer_build_authorization_header($oauth) {
  $r = 'Authorization: OAuth ';
  $values = array();
  foreach ($oauth as $key => $value)
    $values[] = "$key=\"" . rawurlencode($value) . "\"";
  $r .= implode(', ', $values);
  return $r;
}

The response I get is:

stdClass Object
(
    [errors] => Array
        (
            [0] => stdClass Object
                (
                    [code] => 32
                    [message] => Could not authenticate you.
                )

        )

)

Despite the fact that I am able to use this console:

https://dev.twitter.com/rest/tools/console

To perform a query.

What am I doing wrong?

The HTTP header for OAuth that Twitter seems to use uses the same values as what I sent.

Can't see anything obviously wrong, but I used the following when building mine (different language):

https://dev.twitter.com/oauth/overview/authorizing-requests

and

https://dev.twitter.com/oauth/overview/creating-signatures

Use the exact keys and parameters as that article and then verify your header looks exactly the same.

Make sure you don't have your keys switched and no spaces.

Another common problem is with server clock differences. Look at the time from Twitter's response header and make sure your time is the same.