since a week I'm trying to manage "Login with twitter" functionality in my website. I can not complete even the first step: 'obtain a request token'. I recieve:
"Failed to validate oauth signature and token"
I'm doing everything like said here: https://dev.twitter.com/docs/auth/implementing-sign-twitter
And creating a signature like here: https://dev.twitter.com/docs/auth/creating-signature
My code:
date_default_timezone_set('UTC');
$oauth_callback = 'http://mydomain.net'; // calls are made from index.php
$url = 'https://api.twitter.com/oauth/request_token';
$oauth_consumer_key = '***';
$oauth_nonce = str_shuffle(trim(base64_encode(time()), '='));
$oauth_signature_method = 'HMAC-SHA1';
$oauth_timestamp = time();
$oauth_token = '***';
$oauth_version = '1.0';
$HTTPMethod = 'POST';
$BaseURL = 'https://api.twitter.com/oauth/request_token';
$consumer_secret = '***';
$access_token_secret = '***';
$params = array(
'oauth_consumer_key' => $oauth_consumer_key,
'oauth_nonce' => $oauth_nonce,
'oauth_signature_method' => $oauth_signature_method,
'oauth_timestamp' => $oauth_timestamp,
'oauth_token' => $oauth_token,
'oauth_version' => $oauth_version,
'oauth_callback' => $oauth_callback
);
function parameter_string (array $params)
{
$temp_array = array();
$parameter_string = '';
while (current($params)) {
$temp_array[rawurlencode(key($params))] = rawurlencode(current($params));
next($params);
}
ksort($temp_array);
foreach ($temp_array as $key => $value) {
$parameter_string .= '&' . $key . '=' . $value;
}
return trim($parameter_string, '&');
}
$parameter_string = parameter_string($params);
function signature_base_string ( $HTTPMethod, $BaseURL, $parameter_string)
{
$signature_base_string = $HTTPMethod . '&' . rawurlencode($BaseURL) . '&' . rawurlencode($parameter_string);
return $signature_base_string;
}
$signature_base_string = signature_base_string ($HTTPMethod, $BaseURL, $parameter_string);
function signing_key($consumer_secret)
{
return rawurlencode($consumer_secret) . '&';
}
$signing_key = signing_key($consumer_secret);
$oauth_signature = base64_encode(hash_hmac('SHA1', $signature_base_string, $signing_key, true));
$header[] = 'Authorization: OAuth '.
'oauth_callback="'.rawurlencode($oauth_callback).'", '.
'oauth_consumer_key="'.rawurlencode($oauth_consumer_key).'", '.
'oauth_nonce="'.rawurlencode($oauth_nonce).'", '.
'oauth_signature="'.rawurlencode($oauth_signature).'", '.
'oauth_signature_method="'.rawurlencode('HMAC-SHA1').'", '.
'oauth_timestamp="'.rawurlencode($oauth_timestamp).'", '.
'oauth_version="'.rawurlencode('1.0').'"';
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_HTTPHEADER => $header,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true
);
$c = curl_init();
$d = curl_setopt_array($c, $options);
$response = curl_exec($c);
echo '<pre>';
print_r($response);
echo '</pre>';
echo '<pre>';
print_r (curl_getinfo($c));
echo '</pre>';
curl_close($c);
The request header looks like this:
POST /oauth/request_token HTTP/1.1
Host: api.twitter.com
Accept: */*
Authorization: OAuth oauth_callback="http%3A%2F%2Fmydomain.net", oauth_consumer_key="***", oauth_nonce="2DHXEIM541CPONJS3UFRQ79G8W0KA6LYZBT", oauth_signature="***", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1379239417", oauth_version="1.0"
Content-Length: 0
Content-Type: application/x-www-form-urlencoded
Please help!
Related
I want to get tiwtter top trend list 10 or 30, world and locations, for example, kiev or London etc. I tried with this.
https://api.twitter.com/1.1/trends/place.json
https://api.twitter.com/1.1/trends/place.json?id=1
and result ;
{
"errors": [
{
"code": 215,
"message": "Bad Authentication data."
}
]
}
pls help about this subject in php , dont work other resources , version 1.1 api !
i did ;
<?php
error_reporting(0);
$twitterurl = "https://api.twitter.com/1.1/trends/place.json";
$oauth_access_token = "XXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$oauth_access_token_secret ="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$consumer_key = "XXXXXXXXXXXXXXXXXXXXXXXXX";
$consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$woeid='1';
/*
* World= 1
*/
$oauth = array( 'oauth_consumer_key' => $consumer_key, 'oauth_nonce' => time(),'id' => $woeid, 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_nonce' => time(),'oauth_token' => $oauth_access_token, 'oauth_timestamp' => time(), 'oauth_version' => '1.0');
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;
}
$base_info = buildBaseString($twitterurl, '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;
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $twitterurl . "?id=$woeid",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$trends = json_decode($json,true);
//print_r($json); //if u see all data add$trend up line $trends =json_decode($json); and delete this line down lines change !
$arrtrends=array();
for($i=0;$i<10;$i++){
$arrtrends[]=$trends[0][trends][$i][name];
}
$locationname=$trends[0][locations][0][name];
//
print_r($arrtrends);
echo "Location: $locationname";
I've set up the twitter API for getting tweets from a username being matthutchings95 and on this account I have posted 10 times with test tweets. Now the page is reading these fine but I then made a second twitter account dannybots111 and wrote a tweet mentioning the first account. But this isn't showing on the page have I got to change the screen_name to something else? I thought this did both mentions and tweets from that account.
<?
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;
}
function returnTweet(){
$oauth_access_token = "1140776708-Z03sg5mVXqc4il9ff1EfBcbE5aycwlfslDni2Lt";
$oauth_access_token_secret = "gr6RbsvIq9kUoREjVkwlOgHSwJJU7do91NpCz87PKNMOz";
$consumer_key = "Ub3R51niGzjJIFQtCDrb7tvjJ";
$consumer_secret = "T74d8SJYDET9n8T4PDI7aobNwo9M6fkmQI0hHhWd9ntzY9y5hL";
$twitter_timeline = "user_timeline"; // mentions_timeline / user_timeline / home_timeline / retweets_of_me
// create request
$request = array(
'screen_name' => 'matthutchings95',
'count' => '3'
);
$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'
);
// merge request and oauth to one array
$oauth = array_merge($oauth, $request);
// do some magic
$base_info = buildBaseString("https://api.twitter.com/1.1/statuses/$twitter_timeline.json", '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 request
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => "https://api.twitter.com/1.1/statuses/$twitter_timeline.json?". http_build_query($request),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return json_decode($json, true);
}
$tweet = returnTweet();
echo "Tweet No.1: " .$tweet[0]["text"];
echo '<br><br>';
echo "Tweet No.2: " .$tweet[1]["text"];
echo '<br><br>';
echo "Tweet No.3: " .$tweet[2]["text"];
echo '<br><br>';
echo '<br><br>';
print_r(returnTweet());
?>
The user_timeline will only return Tweets posted by the user specified in the screen_name value. You would need to read the mentions_timeline, and you would need to be authenticated as the mentioned user, to see the mentions.
I want to validate twitter usename. I referred this Link
It returns the twitter json array when i tried in local (XAMPP) server. But it returns empty in server.
FYI: CURL is enabled in the server.
How to solve this.
Updates:
protected 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));
}
protected function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
public function returnTweet(){
$oauth_access_token = "myToken";
$oauth_access_token_secret = "MySecrekKey";
$consumer_key = "My Consumer Key";
$consumer_secret = "My Consumer Secret Key";
$twitter_timeline = "user_timeline";
$twusername = 'myUsername';
// create request
$request = array(
'screen_name' => $twusername,
'count' => '3'
);
$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'
);
$oauth = array_merge($oauth, $request);
$base_info = Mage::getModel('Namespace/Filename')->buildBaseString("https://api.twitter.com/1.1/statuses/$twitter_timeline.json", '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;
$header = array(Mage::getModel('socialtabs/SocialTab')->buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => "https://api.twitter.com/1.1/statuses/$twitter_timeline.json?". http_build_query($request),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return $json;
}
I've been trying to access the new twitter api (1.1) for 3 days now, the new oauth system and i are not compatible. I only want to grab 3 of my latest tweets (which are public so why the need for oauth over simple rss is annoying)
<?php
$header = 'GET /statuses/user_timeline.json?screen_name=NAME&count=3&include_rts=false HTTP/1.1
Host: https://www.twtter.com:443
Authorization: OAuth realm="https://https://www.twtter.com/statuses/user_timeline.json",
oauth_consumer_key="key",
oauth_token="mytoken",
oauth_nonce="",
oauth_timestamp="0",
oauth_signature_method="HMAC-SHA1",
oauth_version="1.0",
oauth_signature="O4yJhqnYlxMP5U97rJ%2F4UuLjh84%3D"';
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=NAME&count=3&include_rts=false";
$options = array(
CURLOPT_HTTPHEADER => $header,
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);
?>
The header seems to be the problem ( i generated it using: http://hueniverse.com/oauth/guide/authentication/ )
NAME, MYTOKEN, KEY are placeholders for posts' sake.
Can anyone see what the problem is?
This is what I use. Just call returnTweet()
For more info about 'user_timeline', visit API documentation
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;
}
function returnTweet(){
$oauth_access_token = "xxx";
$oauth_access_token_secret = "xxx";
$consumer_key = "xxx";
$consumer_secret = "xxx";
$twitter_timeline = "user_timeline"; // mentions_timeline / user_timeline / home_timeline / retweets_of_me
// create request
$request = array(
'trim_user' => 1,
'screen_name' => 'budidino',
'count' => '3'
);
$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'
);
// merge request and oauth to one array
$oauth = array_merge($oauth, $request);
// do some magic
$base_info = buildBaseString("https://api.twitter.com/1.1/statuses/$twitter_timeline.json", '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 request
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => "https://api.twitter.com/1.1/statuses/$twitter_timeline.json?". http_build_query($request),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return json_decode($json, true);
}
I hope it helps ;)
I'm using this peice of code to try and get all my latest tweets for printing on my site but it's returning an error about being not authenticated. I've created an app in the Dev section and get my consumer and OAuth keys, and added them in the right place in the code.
<?php
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/user_timeline.json?screen_name=jameskrawczyk";
$oauth_access_token = "SECURITY";
$oauth_access_token_secret = "SECURITY";
$consumer_key = "SECURITY";
$consumer_secret = "SECURITY";
$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;
$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, true);
foreach ($twitter_data as $elem)
{
print_r($elem);
echo '<br>';
}
Error returned on page
Array ( [0] => Array ( [message] => Could not authenticate you [code] => 32 ) )
Answer seemed to be that the line
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=jameskrawczyk";
should not have the ?screen_name=jameskrawczyk at the end.
You have to use an library to get data from twitter
"themattharris" is a good and famous library download it from here