使用PHP在twitter中使用OAuth发送多个帐户


Tweet multiple accounts using OAuth in twitter using PHP?

我想要一个应用程序,其中我有一个推文文本框,当用户点击提交按钮的推文将被发布到多个推特帐户。

我已经使这个工作,但只在一个帐户。使用这个例子:https://dev.twitter.com/docs/auth/oauth/single-user-with-examples

我也有这个代码:

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') );

当$oauth_token和$oauth_token_secret值可以在我的应用程序详细信息(https://dev.twitter.com/apps/1482596/show)中找到时,此代码有效,但是当我使用不同的twitter帐户时,它将转到授权页面,然后重定向回我的应用程序并说:

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

这是我的代码,当发生这种情况时,我只得到一旦它返回到我的应用程序生成的$_SESSION:

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') );

我不知道为什么会这样。请分享一些想法如何实现我想要的应用程序。任何想法将大大赞赏和奖励!谢谢!:)

如果这是@Abraham的TwitterOAuth并且您正在使用默认的callback.php,则需要将会话行更改为:

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

必须提到oauth_token和oauth_token_secret在应用程序详细信息中称为TokenKey和Tokensecret,对于新用户,您应该在他们点击授权链接后从twitter API获得$oauth_token$oauth_token_secret

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"]  );
}
当然,您可以使用其他函数凝聚来改进代码,并为每个新的授权用户添加DB表,
但请记住,请求一个新的request_token将生成一个新的oauth_token &Oauth_token_secret密钥。最后一件最让我困惑的事情是,当发布其他用户状态时,CONSUMER_KEY, CONSUMER_SECRET twitter使用相同的密钥进行主应用程序授权和其他用户授权,我知道有些人仍然陷入这种情况。