duanlaiquan8174 2015-10-31 05:36
浏览 61

Twitter API不要求用户访问权限

I am a newbie using twitter API and trying to fetch the user's timeline posts using the twitter API. I have tries this piece of code to achieve this.

It is showing me all my account's timeline data but not asking for user permission access or login credentials as technically i am not logged into my account right now still it is showing me all my account's data.

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;
}

$url = "https://api.twitter.com/1.1/statuses/home_timeline.json"; 
$oauth_access_token = "ACCESS TOKEN";
$oauth_access_token_secret = "ACCESS TOKEN SECRET";
$consumer_key = "APP KEY";
$consumer_secret = "APP KEY SECRET";

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

$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), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
                  //CURLOPT_POSTFIELDS => $postfields,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url,
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_SSL_VERIFYPEER => false);

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

$twitter_data = json_decode($json);

//print it out
echo "<pre>"; print_r ($twitter_data);

I have found the code on this post

Any help will be much appreciated.

Thanks

  • 写回答

1条回答 默认 最新

  • douya5331 2015-10-31 08:54
    关注

    You are already using the Access Token and Access Token Secret for your account that you got from your Twitter Application Settings page, so you are already logged in. That is all the user authentication data you need to access your account through API.

    Only for reading someone else's protected data you'll need access token and secret for their account, which you can obtain by implementing Twitter OAuth login flow (redirecting them to twitter and asking them to grant your app, access to their account).

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)