Can't authenticate with OAUTH for 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 = ""; // 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,

  $feed = curl_init();
  curl_setopt_array($feed, $options);
  $json = curl_exec($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();
  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:

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):


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.


ed cure - generic cialis calcium channel blockers drugs and cialis use <a href="">generic cialis</a> - guestbook.php?action= buy generic cialis daily online 5mg

cialis tadalafil 20mg reviews clever.cgi?page= - generic cialis cialis for daily use free trial queries <a href="">generic cialis</a> - cialis and alcoholic drinks password cialis 10mg price sort by