【问题标题】:Twitter oAuth: getting an error 215Twitter oAuth:收到错误 215
【发布时间】:2013-02-19 20:25:33
【问题描述】:

我的一门课程中有以下代码:

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

【问题讨论】:

    标签: php oauth twitter


    【解决方案1】:

    你看过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',
    ));
    

    【讨论】:

    • 是的,我看过 Codebird,但似乎无法让它工作。如果您有使用 Codebird 的示例,请发布
    • 嗯,这行得通,但我尝试了 users_lookup,但那行不通。我相信这是一个 Twitter 错误: $result = $cb->users_lookup(array('user_id'=>'12345'));给出错误 34,而 $result = $cb->users_lookup(array('screen_name'=>'hansnilsson'));生成正确的配置文件。我将错误发布到 Twitter 讨论组。这个电话在几周前还有效。
    • 话虽如此,我仍在寻找我的问题的答案
    • user_id 应该是一个整数?除此之外,希望你能找到正确的答案。
    • 我建议您阅读 users/lookup API 文档。 user_id 字段是一个逗号分隔的列表,最多包含 100 个 ID。
    猜你喜欢
    • 2018-12-27
    • 1970-01-01
    • 2010-11-19
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2016-08-16
    • 2014-06-01
    相关资源
    最近更新 更多