douwei3172 2016-03-23 11:29
浏览 107

Twitter API - 无法验证 - 拔掉头发

Trying to get a list of a twitter user's followers.

Tried both a hand-coded version and using codebird - the codebird version simply tries to post a test tweet.

Each time I get the 'Could not authenticate you' error message.

I've regenerated all the keys a few times and retried - still get same error.

I've tried setting up a new account, setting up a new app in that account and still, get 'Could not authenticate you' message.

Both of these versions worked when I used them last week - this week, neither of them work.

Any help greatly appreciated - here's the code, Codebird first:

// require codebird
require_once('codebird/src/codebird.php');

\Codebird\Codebird::setConsumerKey("consumerkey", "consumersecret");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("oauthtoken", "oauthsecret");

$params = array(
  'status' => 'another Auto Post on Twitter with PHP #php #twitter');
$reply = $cb->statuses_update($params);

var_dump($reply);

+++++++++++++++ Here's the hand-coded:

function buildBaseString($baseURI, $method, $params) {
    $r = array();
    ksort($params);
    foreach($params as $key=>$value){
        $r[] = "$key=" . rawurlencode($value);
    }
    return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}


function buildAuthorizationHeader($oauth) {
    $r = 'Authorization: OAuth ';
    $values = array();
    foreach($oauth as $key=>$value)
        $values[] = "$key=\"" . rawurlencode($value) . "\"";
    $r .= implode(', ', $values);
    return $r;
}

$myaccount = "@twitterdev";
$cursor = -1;

$url = "https://api.twitter.com/1.1/followers/list.json";

$consumer_key = "consumerkey";
$consumer_secret = "consumersecret";

$oauth_access_token = "accesstoken";
$oauth_access_token_secret = "accesssecret";

$oauth = array('count' => 200,
               'cursor' => $cursor,
               'oauth_consumer_key' => $consumer_key,
               'oauth_nonce' => time(),
               'oauth_signature_method' => 'HMAC-SHA1', 
                'oauth_timestamp' => time(),
               'oauth_token' => $oauth_access_token,
                'oauth_version' => '1.0',
                'screen_name' => $myaccount
             );                    

$base_info = buildBaseString($url, 'GET', $oauth);

$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;


// Make requests

$header = array(buildAuthorizationHeader($oauth));
foreach($header as $key=>$value){
        echo "$key: $value<BR>";
    }

$options = array( CURLOPT_HTTPHEADER => $header,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url.'?screen_name='.$myaccount.'&cursor='.$cursor.'&count=200',
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_SSL_VERIFYPEER => TRUE);

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

var_dump($json);

$twitter_data = json_decode($json,true);

foreach($twitter_data AS $key => $value){
    if($key == 'next_cursor_str'){
        $next_cursor = $value;
    }
    if($key == 'users'){
        foreach ($value AS $key2 => $value2){
            if($key == 'users'){
                $id = $value2["id"];
                $name = $value2["name"];
                $screen_name = $value2["screen_name"];
                $description = $value2["description"];
                $followers_count = $value2["followers_count"];
                $friends_count = $value2["friends_count"];
                $statuses_count = $value2["statuses_count"];
                $statustime = '00-00-00 00:00:00';
                $statustime = @$value2["status"]["created_at"];
                $statime = date('Y-m-d H:i:s', strtotime($statustime));
                $allfollow[$id]["name"] = mysql_real_escape_string(@$name);
                $allfollow[$id]["screen_name"] = mysql_real_escape_string(@$screen_name);
                $allfollow[$id]["statuses_time"] = mysql_real_escape_string(@$statime);
                $allfollow[$id]["description"] = mysql_real_escape_string(@$description);
                $allfollow[$id]["followers_count"] = @$followers_count;
                $allfollow[$id]["friends_count"] = @$friends_count;
                $allfollow[$id]["statuses_count"] = @$statuses_count;
            }    
        }
    }
}

echo "<table>";
echo "<th>Name</th><th>Screen name</th><th>Description</th><th>Followers</th><th>Friends</th><th>Statuses</th><th>Time</th>";
foreach($allfollow AS $key => $value){
    echo "<tr>";
    echo "<td>{$value[name]}</td>
          <td>{$value[screen_name]}</td>
          <td>{$value[description]}</td>
          <td>{$value[followers_count]}</td>
          <td>{$value[friends_count]}</td>
          <td>{$value[statuses_count]}</td>
          <td> {$value[statuses_time]}</td>";
    echo "</tr>";
}
echo "</table>";
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?