twitter api 1.1的Oauth头问题


Oauth header issue with twitter api 1.1

我已经尝试访问新的twitter api(1.1) 3天了,新的oauth系统和我不兼容。我只想抓取我最近的3条推文(这是公开的,所以为什么需要oauth而不是简单的rss是令人讨厌的)

<?php
$header = 'GET /statuses/user_timeline.json?screen_name=NAME&count=3&include_rts=false HTTP/1.1
Host: https://www.twtter.com:443
Authorization: OAuth realm="https://https://www.twtter.com/statuses/user_timeline.json",
    oauth_consumer_key="key",
    oauth_token="mytoken",
    oauth_nonce="",
    oauth_timestamp="0",
    oauth_signature_method="HMAC-SHA1",
    oauth_version="1.0",
    oauth_signature="O4yJhqnYlxMP5U97rJ%2F4UuLjh84%3D"';
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=NAME&count=3&include_rts=false";
$options = array( 
            CURLOPT_HTTPHEADER => $header,
            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);
?>

头似乎是问题(我生成它使用:http://hueniverse.com/oauth/guide/authentication/)

NAME, MYTOKEN, KEY是帖子的占位符。

谁能看出问题是什么吗?

这是我使用的。只要调用returnTweet()

有关'user_timeline'的更多信息,请访问API文档

function buildBaseString($baseURI, $method, $params) {
    $r = array();
    ksort($params);
    foreach($params as $key=>$value){
        $r[] = "$key=" . rawurlencode($value);
    }
    return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
    $r = 'Authorization: OAuth ';
    $values = array();
    foreach($oauth as $key=>$value)
        $values[] = "$key='"" . rawurlencode($value) . "'"";
    $r .= implode(', ', $values);
    return $r;
}    
function returnTweet(){
        $oauth_access_token         = "xxx";
        $oauth_access_token_secret = "xxx";
        $consumer_key               = "xxx";
        $consumer_secret            = "xxx";
        $twitter_timeline           = "user_timeline";  //  mentions_timeline / user_timeline / home_timeline / retweets_of_me
        //  create request
            $request = array(
                'trim_user'     => 1,
                'screen_name'   => 'budidino',
                'count'         => '3'
            );
        $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'
        );
        //  merge request and oauth to one array
            $oauth = array_merge($oauth, $request);
        //  do some magic
            $base_info              = buildBaseString("https://api.twitter.com/1.1/statuses/$twitter_timeline.json", '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 request
            $header = array(buildAuthorizationHeader($oauth), 'Expect:');
            $options = array( CURLOPT_HTTPHEADER => $header,
                              CURLOPT_HEADER => false,
                              CURLOPT_URL => "https://api.twitter.com/1.1/statuses/$twitter_timeline.json?". http_build_query($request),
                              CURLOPT_RETURNTRANSFER => true,
                              CURLOPT_SSL_VERIFYPEER => false);
            $feed = curl_init();
            curl_setopt_array($feed, $options);
            $json = curl_exec($feed);
            curl_close($feed);
        return json_decode($json, true);
    }