PinoyStackOverflower
PinoyStackOverflower

Reputation: 5302

Tweet multiple accounts using OAuth in twitter using PHP?

I wanted an application wherein I have a tweet textbox and when the user click on the submit button the tweet will be posted to multiple twitter account.

I already made this working but only on a single account. Using this example: https://dev.twitter.com/docs/auth/oauth/single-user-with-examples

I also have this code:

function getConnectionWithAccessToken($oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
  return $connection;
}

$connection = getConnectionWithAccessToken('myAuthTokenInAppDetail', 'myAuthTokenSecretInAppDetail');
$content = $connection->post('statuses/update', array('status'=>'hello world') );

This code works when the $oauth_token and $oauth_token_secret values are the one that can be found in my App details( https://dev.twitter.com/apps/1482596/show ), but when I use a different twitter account, it will go the the Authorize Page then Redirect Back to my application and says:

stdClass Object
(
    [error] => Could not authenticate you.
    [request] => /1/statuses/update.json
)

This is my code when this happens, I just get the $_SESSION that was being generated once it got back to my application:

function getConnectionWithAccessToken($oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
  return $connection;
}

$oauth_token = $_SESSION['oauth_token'];
$oauth_token_secret = $_SESSION['oauth_token_secret'];

$connection = getConnectionWithAccessToken($oauth_token, $oauth_token_secret);
$content = $connection->get("statuses/home_timeline");
$content = $connection->post('statuses/update', array('status'=>'hello world') );

I don't know why this happens. Kindly share some idea how to implement that app that I wanted. Any idea will be greatly appreciated and rewarded! Thanks! :)

Upvotes: 1

Views: 1141

Answers (2)

Salem
Salem

Reputation: 774

just have to mention oauth_token and oauth_token_secret in App details are called TokenKey and Tokensecret and for the new users you should get back $oauth_token and $oauth_token_secret from twitter API after they click on authorization link

if ( empty($_GET["oauth_token"]) && empty($_SESSION['oauth_token'])    ) 
//you could add another conditions here using cookies or using DB .
 { 
    $request_token = $connection->oauth('oauth/request_token', 
            array('oauth_callback' => OAUTH_CALLBACK));
    $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
    echo " <br/><a href='$url'>Sign-in</a> <br/>";
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];  
    }

else {

$oauth_verifier =  $_SESSION['oauth_token']   ;  
$oauth_token = $_SESSION['oauth_token_secret'] ;
// return user details 
$request_token2 = $connection->oauth('oauth/access_token',    
 array(  'oauth_verifier' => $oauth_verifier  , 'oauth_token' => $oauth_token  )  )  ;
$connection2=new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, 
$request_token2[oauth_token] , $request_token2[oauth_token_secret] );

// send out other user new status .
$statues = $connection2->post("statuses/update",  ["status" => "hello world"]  );
}

of course you can improve that code using other function condensations and add DB table for each new authorized user ,
but keep in mind requesting a new request_token will generate new oauth_token & oauth_token_secret keys each time . last one of most thing confused me when post other user status CONSUMER_KEY, CONSUMER_SECRET twitter uses the same key for main app authorization and other users authorization , i know some people still fall into this .

Upvotes: 1

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29985

If this is @Abraham's TwitterOAuth and you're using the default callback.php, you'll need to change your session lines to:

$oauth_token = $_SESSION['access_token']['oauth_token'];
$oauth_token_secret = $_SESSION['access_token']['oauth_token_secret'];

Upvotes: 1

Related Questions