使用PHP在twitter中使用OAuth推送多个帐户?
我想要一个应用程序,其中我有一个tweet文本框,当用户点击提交按钮时,tweet将被发布到多个twitter帐户使用PHP在twitter中使用OAuth推送多个帐户?,php,twitter,oauth,Php,Twitter,Oauth,我想要一个应用程序,其中我有一个tweet文本框,当用户点击提交按钮时,tweet将被发布到多个twitter帐户 我已经做到了这一点,但只有一个帐户。使用此示例: 我还有以下代码: function getConnectionWithAccessToken($oauth_token, $oauth_token_secret) { $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oaut
我已经做到了这一点,但只有一个帐户。使用此示例: 我还有以下代码:
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值是可以在我的应用程序详细信息()中找到的值时,此代码会起作用,但当我使用其他twitter帐户时,它会转到授权页面,然后重定向回我的应用程序并显示:
stdClass Object
(
[error] => Could not authenticate you.
[request] => /1/statuses/update.json
)
这是我的代码当发生这种情况时,我只会得到返回到我的应用程序后生成的$\u会话
:
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's 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"] );
}
if(空($\u GET[“oauth\u令牌])&空($\u会话['oauth\u令牌]))
//您可以使用cookie或DB在此处添加其他条件。
{
$request\u token=$connection->oauth('oauth/request\u token',
数组('oauth_回调'=>oauth_回调));
$url=$connection->url('oauth/authorize',array('oauth\u token'=>$request\u token['oauth\u token']);
回声“
”;
$\u会话['oauth\u令牌']=$request\u令牌['oauth\u令牌'];
$\u会话['oauth\u令牌\u密码]=$request\u令牌['oauth\u令牌\u密码];
}
否则{
$oauth_校验器=$_会话['oauth_令牌];
$oauth_token=$_会话['oauth_token_secret'];
//返回用户详细信息
$request\u token2=$connection->oauth('oauth/access\u token',
数组('oauth_校验器'=>$oauth_校验器,'oauth_令牌'=>$oauth_令牌));
$connection2=新的TwitterOAuth(消费者密钥、消费者密钥、,
$request_token2[oauth_token],$request_token2[oauth_token\u secret];
//发送其他用户新状态。
$status=$connection2->post(“状态/更新”、[“状态”=>“你好世界”);
}
当然,您可以使用其他函数压缩来改进代码,并为每个新的授权用户添加DB表,但是请记住,每次请求一个新的request_令牌都会生成新的oauth_令牌&oauth_令牌密钥。
最后一件最让我困惑的事情是,当发布其他用户状态
CONSUMER\u KEY,CONSUMER\u SECRET
twitter使用相同的密钥进行主应用程序授权和其他用户授权时,我知道有些人仍然会陷入这种情况。是的,我使用的是Abrahams TwitterOAuth。我修改了他的代码,因为这似乎是他的代码中的一个bug,因为当我尝试打印\r($\u会话)时,数组中没有“访问\u令牌”,或者它不是嵌套数组。$_会话的结果是这样的:数组{'oauth_token'=>123124125125'oauth_token_secret'=>135r135t13513jlsad}你觉得怎么样?好的,我使用了$oauth_token=$_会话['access_token'['oauth_token']$oauth_token_secret=$\u会话['access_token']['oauth_token_secret']代码>但我仍然得到:
stdClass对象([error]=>无法对您进行身份验证。[request]=>/1/statuses/update.json)
您是否忘记了会话启动()代码>?不。我没有忘记放置session\u start()'