2013-12-12 21 views
0

昨天,我發佈了有關Twitter的API約Search for Mentions of a Topic。我問了要使用哪個URL,因爲我認爲我做錯了。修改此代碼以檢索提及或Hashtags

事實證明,我最初猜測要使用哪個URL是正確的(因爲它應該是,因爲我用Twitter Dev Console來獲取URL。問題似乎是在我找到的代碼中實際使用URL 。我對Twitter API的理解非常有限,但是現在看起來很難做簡單的事情......

我想用this code (which I found in a blog post)來訪問推文當我用這個加載一個PHP頁面代碼(當然有我的信息),我從我的個人帳戶看到推文沒有問題(當然,我的帳戶上有一個應用程序),但是當我嘗試更改代碼中的兩個URL時本身,我得到{"errors":[{"message":"Could not authenticate you","code":32}]}那麼我還需要修改代碼?

注意:HERE是我找到代碼的原始博客文章。

編輯:這是我使用完全相同的代碼(當然,減去我的信息):

<?php 

//After you create the app at http://dev.twitter.com, you'll need to get the following four pieces of data from the details tab in your app's page. 
$consumer_key = ''; 
$consumer_secret = ''; 
$access_token = ''; 
$access_token_secret = ''; 

$oauth_hash = 'oauth_consumer_key='.$consumer_key. 
    '&oauth_nonce='.time(). 
    '&oauth_signature_method=HMAC-SHA1&oauth_timestamp='. 
    time().'&oauth_token='.$access_token.'&oauth_version=1.0'; 

// $base = 'GET&'.rawurlencode('http://api.twitter.com/1.1/search/tweets.json?q=apple&result_type=recent&count=10‌'). 
// '&'.rawurlencode($oauth_hash); 
    $base = 'GET&'.rawurlencode('https://api.twitter.com/1.1/statuses/user_timeline.json'). 
    '&'.rawurlencode($oauth_hash); 


$key = rawurlencode($consumer_secret).'&'. rawurlencode($access_token_secret); 

$signature = base64_encode(hash_hmac('sha1', $base, $key, true)); 
$signature = rawurlencode($signature); 

$oauth_header = 'oauth_consumer_key="'.$consumer_key.'", 
    oauth_nonce="' . time() . '",oauth_signature="' . $signature . '", 
    oauth_signature_method="HMAC-SHA1", 
    oauth_timestamp="' . time() . '", 
    oauth_token='.$access_token.', 
    oauth_version="1.0", '; 

$curl_header = array("Authorization: Oauth {$oauth_header}", 'Expect:'); 


$curl_request = curl_init(); 
    curl_setopt($curl_request, CURLOPT_HTTPHEADER, $curl_header); 
    curl_setopt($curl_request, CURLOPT_HEADER, false); 
    // curl_setopt($curl_request, CURLOPT_URL, 'http://api.twitter.com/1.1/search/tweets.json?q=apple&result_type=recent&count=10‌'); 
    curl_setopt($curl_request, CURLOPT_URL, 'https://api.twitter.com/1.1/statuses/user_timeline.json'); 
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, false); 
$json = curl_exec($curl_request); 
    curl_close($curl_request); 
echo $json; 
+0

您是否嘗試過在代碼的原始博客文章?該錯誤表明您沒有正確傳遞oauth標記 –

回答

0

個人而言,我會嘗試Twitter的推薦PHP庫之一(這個看起來輕巧好:https://github.com/J7mbo/twitter-api-php) 。

在其他新聞中,有一個跟進後您正在與工作的一個糾正了這個問題:http://blog.jacobemerick.com/web-development/passing-extra-parameters-to-twitter-via-oauth/

哈希變化:

$oauth_hash = ''; 
$oauth_hash .= 'count=TOTAL_COUNT_YOU_WANT&'; 
$oauth_hash .= 'oauth_consumer_key=YOUR_CONSUMER_KEY&'; 
$oauth_hash .= 'oauth_nonce=' . time() . '&'; 
$oauth_hash .= 'oauth_signature_method=HMAC-SHA1&'; 
$oauth_hash .= 'oauth_timestamp=' . time() . '&'; 
$oauth_hash .= 'oauth_token=YOUR_ACCESS_TOKEN&'; 
$oauth_hash .= 'oauth_version=1.0&'; 
$oauth_hash .= 'screen_name=SCREEN_NAME_HERE'; 

頭更改:

$oauth_header = ''; 
$oauth_header .= 'count="TOTAL_COUNT_YOU_WANT", '; 
$oauth_header .= 'oauth_consumer_key="YOUR_CONSUMER_KEY", '; 
$oauth_header .= 'oauth_nonce="' . time() . '", '; 
$oauth_header .= 'oauth_signature="' . $signature . '", '; 
$oauth_header .= 'oauth_signature_method="HMAC-SHA1", '; 
$oauth_header .= 'oauth_timestamp="' . time() . '", '; 
$oauth_header .= 'oauth_token="YOUR_ACCESS_TOKEN", '; 
$oauth_header .= 'oauth_version="1.0", '; 
$oauth_header .= 'screen_name="SCREEN_NAME_HERE"'; 

捲曲變化:

curl_setopt($curl_request, CURLOPT_URL, 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=TOTAL_COUNT_YOU_WANT&screen_name=SCREEN_NAME_HERE');

相關問題