I am trying to call following Twitter's API to get a list of followers for a user.
http://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=username
And I am getting this error message in response.
{ code = 215; message = "Bad Authentication data"; }
I can't seem to find the documentation related to this error code. Anyone has any idea about this error?
This could be due to missing or incorrect authentication credentials. This may also be returned in other undefined circumstances. Check that you are using the correct authentication method and that your credentials are correct.
This means that the only requests you can make to a Twitter API must not require an authenticated user. With application-only authentication, you can perform actions such as: Pull user timelines. Access friends and followers of any account.
The Twitter API lets you read and write Twitter data. Thus, you can use it to compose tweets, read profiles, and access your followers' data and a high volume of tweets on particular subjects in specific locations. API stands for Application Programming Interface.
Elevated: Use case review required, apply for access through your developer account, 3 Apps/environments (1 Project) up to 2M Tweets per month on data endpoints. Enterprise: Use case review required, apply for Enterprise access, 3+ Apps/environments (1 Project). Access begins at 10M Tweets per month.
A very concise code without any other php file include of oauth etc. Please note to obtain following keys you need to sign up with https://dev.twitter.com and create application.
<?php $token = 'YOUR_TOKEN'; $token_secret = 'YOUR_TOKEN_SECRET'; $consumer_key = 'CONSUMER_KEY'; $consumer_secret = 'CONSUMER_SECRET'; $host = 'api.twitter.com'; $method = 'GET'; $path = '/1.1/statuses/user_timeline.json'; // api call path $query = array( // query parameters 'screen_name' => 'twitterapi', 'count' => '5' ); $oauth = array( 'oauth_consumer_key' => $consumer_key, 'oauth_token' => $token, 'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended 'oauth_timestamp' => time(), 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_version' => '1.0' ); $oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting $query = array_map("rawurlencode", $query); $arr = array_merge($oauth, $query); // combine the values THEN sort asort($arr); // secondary sort (value) ksort($arr); // primary sort (key) // http_build_query automatically encodes, but our parameters // are already encoded, and must be by this point, so we undo // the encoding step $querystring = urldecode(http_build_query($arr, '', '&')); $url = "https://$host$path"; // mash everything together for the text to hash $base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring); // same with the key $key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret); // generate the hash $signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true))); // this time we're using a normal GET query, and we're only encoding the query params // (without the oauth params) $url .= "?".http_build_query($query); $url=str_replace("&","&",$url); //Patch by @Frewuill $oauth['oauth_signature'] = $signature; // don't want to abandon all that work! ksort($oauth); // probably not necessary, but twitter's demo does it // also not necessary, but twitter's demo does this too function add_quotes($str) { return '"'.$str.'"'; } $oauth = array_map("add_quotes", $oauth); // this is the full value of the Authorization line $auth = "OAuth " . urldecode(http_build_query($oauth, '', ', ')); // if you're doing post, you need to skip the GET building above // and instead supply query parameters to CURLOPT_POSTFIELDS $options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"), //CURLOPT_POSTFIELDS => $postfields, CURLOPT_HEADER => false, CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false); // do our business $feed = curl_init(); curl_setopt_array($feed, $options); $json = curl_exec($feed); curl_close($feed); $twitter_data = json_decode($json); foreach ($twitter_data as &$value) { $tweetout .= preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", '<a href="http://$2$3" target="_blank">$1$2$4</a>', $value->text); $tweetout = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $tweetout); $tweetout = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $tweetout); } echo $tweetout; ?>
Regards
The only solution I've found so far is:
Using this tokens you can make authenticated requests on behalf of user. You can do it with the same library.
// Arguments 1 and 2 - your application static tokens, 2 and 3 - user tokens, received from Twitter during authentification $connection = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, $tokens['oauth_token'], $tokens['oauth_token_secret']); $connection->host = 'https://api.twitter.com/1.1/'; // By default library uses API version 1. $friendsJson = $connection->get('/friends/ids.json?cursor=-1&user_id=34342323');
This will return you list of user's friends.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With