duanpin2034 2015-07-30 15:40
浏览 94

无法使用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.

  • 写回答

1条回答 默认 最新

  • douyong5476 2015-08-01 05:13
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示