2013-02-19 2 views
0

я следующий код в одном из моих классов:Twitter OAuth: получаю сообщение об ошибке 215

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 и твиттер-обертки, но не похоже, что любой из них полностью перенесен в 1.1 API).

ответ

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', 
)); 
+0

Да, я смотрел на Codebird, и я не могу заставить его работать. Если у вас есть пример для работы с Codebird, отправьте сообщение – EastsideDeveloper

+0

Я обновил свой ответ на простом примере. –

+0

Хммм, это работает, но я попробовал users_lookup, и это не работает. Я считаю, что это ошибка Twitter: $ result = $ cb-> users_lookup (array ('user_id' => '12345')); дает ошибку 34, а $ result = $ cb-> users_lookup (array ('screen_name' => 'hansnilsson')); создает правильный профиль. Я отправил ошибку в дискуссионную группу Twitter. Этот звонок работал пару недель назад. – EastsideDeveloper

Смежные вопросы