Twitter oAuth:收到错误215

时间:2013-02-19 11:51:22

标签: php oauth twitter

我的一个班级中有以下代码:

public function build_httpheader ($options = NULL)
{
    if (!$this->oauth_token && !$this->oauth_token_secret && !$this->http_method) {
        throw new Exception('The oauth_token, oauth_token_secret and the http method must be set');
    }
    $url = $this->base_uri;
    $url_curl = $this->base_uri;
    if ($options) {
        $this->options = $options;
        $url .= '?';
        $url_curl .= '?';
        $count = count($options);
        foreach($options as $key => $value) {
            $count = $count--;
            if ($count) {
                $url .= '&'.$key.'='.$value;
                $url_curl .= '&'.$key.'='.rawurlencode($value);
            } else {
                $url .= $key.'='.$value;
                $url_curl .= $key.'='.rawurlencode($value);
            }
        }
    }
    $this->url = $url_curl;
    /**
     * @internal Create a 32 chars unique string to
     * use as the nonce value
     */
    list($usec, $sec) = explode(" ", microtime());
    $usec = str_replace('0.', '', $usec);
    $nonce_str = utf8_encode($sec.$usec.'ABCD'.$sec);
    $oauth_nonce =  md5($nonce_str);
    /**
     * @internal Create the initial oAuth array
     */
    $oauth = array (
        'oauth_consumer_key' => $this->consumer_key,
        'oauth_nonce' => $this->$oauth_nonce,
        'oauth_signature_method' => 'HMAC-SHA1',
        'oauth_token' => $this->oauth_token,
        'oauth_timestamp' => time(),
        'oauth_version' => '1.0'
    );
    /**
     * @internal generate basic info
     */
    $t_oauth = array();
    ksort($oauth);
    foreach($oauth as $key=>$value){
        $t_oauth[] = "$key=" . rawurlencode($value);
    }
    $base_info = $this->http_method."&" . rawurlencode($url) . '&' . rawurlencode(implode('&', $t_oauth));
    $composite_key = rawurlencode($this->consumer_secret) . '&' . rawurlencode($this->oauth_token_secret);
    $oauth['oauth_signature'] = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));

    $oauth_string = 'Authorization: OAuth ';
    $values = array();
    foreach($oauth as $key=>$value) {
        $values[] = "$key=\"" . rawurlencode($value) . "\"";
    }
    $oauth_string .= implode(', ', $values);
    $this->http_headers = array ($oauth_string, 'Expect:');
}

public function tw_curl_api_call ()
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $this->http_headers);
    if ($this->http_method == 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
    }
    if ( ($this->http_method != 'POST') && ($this->http_method != 'GET') ) {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->http_method);
    }
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $this->url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

    $result = json_decode(curl_exec($ch), TRUE);
    $info = curl_getinfo($ch);
    curl_close($ch);
    return array ('result' => $result, 'info' => $info);
}

另一个脚本使用此类如下:

$options = array ('resources' => 'help,users,search,statuses');

$tw_wrapper->build_httpheader ($options);

$results = $tw_wrapper->tw_curl_api_call ();

但是我收到错误215(错误的身份验证数据)。任何想法(我知道有现有的PHP oAuth类和twitter包装器,但似乎没有任何想法完全迁移到1.1 API)。

1 个答案:

答案 0 :(得分:0)

您是否看过Codebird?它支持1.1并且能够满足我的需求。 YMWV当然。

一个简单的例子:

Codebird::setConsumerKey('KEY', 'SECRET');

$cb = Codebird::getInstance();
$cb->setToken('AUTH_KEY', 'AUTH_SECRET');

$result = $cb->statuses_userTimeline(array(
    'include_rts' => true,
    'screen_name' => 'hansnilsson',
));