0

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

Community
  • 1
  • 1
Geetika
  • 790
  • 3
  • 13
  • 29

1 Answers1

0

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).

pii_ke
  • 2,811
  • 2
  • 20
  • 30