2015-10-09 90 views
1

我寫了一個函數從Twitter API獲取請求令牌。我提示以下錯誤:Twitter - 獲取請求令牌:代碼:32

{ 「錯誤」:[{ 「代碼」:32, 「消息」: 「無法驗證你」}]}

我的代碼是這樣的

function beuTwitterOauthToken() 
{ 

    $url = "https://api.twitter.com/oauth/request_token"; 
    $method = "POST"; 

    $oauth_callback = $this->beuTwitterApiCallBack; 
    $oauth_consumer_key = $this->beuTwitterConsumerKey; 
    $oauth_consumer_key_secret = $this->beuTwitterConsumerKeySecret; 
    $oauth_nonce = $this->beuTwitterNonce(); 
    $oauth_signature_method = "HMAC-SHA1"; 
    $oauth_timestamp = time(); 
    $oauth_version = "1.0"; 

    $params = array(
     'oauth_callback'   => rawurlencode($oauth_callback), 
     'oauth_consumer_key'  => rawurlencode($oauth_consumer_key), 
     'oauth_nonce'    => rawurlencode($oauth_nonce), 
     'oauth_signature_method' => rawurlencode($oauth_signature_method), 
     'oauth_timestamp'   => rawurlencode($oauth_timestamp), 
     'oauth_version'    => rawurlencode($oauth_version) 
    ); 

    ksort($params); 

    $parameter_string = http_build_query($params); 

    $base_string = $method . '&'; 
    $base_string .= rawurlencode($url) . '&'; 
    $base_string .= $parameter_string; 

    $oauth_signing_key = rawurlencode($oauth_consumer_key_secret) . '&'; 
    $oauth_signature = base64_encode(hash_hmac('sha1', $base_string, $oauth_signing_key, true)); 

    $params['oauth_signature'] = rawurlencode($oauth_signature); 

    ksort($params); 

     foreach($params as $key=>$value){ 
      $post .= $key.'='.'"'.$value.'", '; 
     } 

    $post = rtrim($post, ", "); 

    $headers = array( 
     "POST /oauth/request_token HTTP/1.1", 
     "User-Agent: Beu", 
     "Host: api.twitter.com", 
     "Accept: */*",   
     "Authorization: OAuth ". $post 
    ); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    $retrievedhtml = curl_exec ($ch); 
    curl_close($ch); 

    return $retrievedhtml; 

} 

我不知道代碼有什麼問題。任何幫助表示讚賞...

回答

0

對我來說是有配置參數有問題:

consumer_key & consumer_secret 

我在Twitter的兩個應用程序,對生產環境中的一組和一個用於開發。我將生產環境中檢索到的訪問令牌與開發環境中爲應用程序提供的消費者密鑰和祕密混合在一起,因此它們在Twitter系統中不匹配。