我已经失去了一天半的时间,现在试图弄清楚为什么Yii在我去Twitter的OAuth页面并回到我的重定向后删除所有会话数据。
这是主SiteController,我在这里访问Twitter。这里我尝试保存oauth_token和token_secret值,以便可以在重定向控制器上使用它们。
function actionTwitter()
{
$consumerKey = "";
$consumerSecret = "";
$connection = new TwitterOAuth($consumerKey, $consumerSecret);
$request_token = $connection->oauth("oauth/request_token", array("oauth_callback" => "http://127.0.0.1/yii/?r=redirect&type=twitter"));
$oauth_token=$request_token['oauth_token'];
$token_secret=$request_token['oauth_token_secret'];
Yii::app()->session['token'] = $oauth_token; // This doesn't save!!
Yii::app()->session['token_secret'] = $token_secret; // This does not save!!
$url = $connection->url("oauth/authorize", array("oauth_token" => $oauth_token));
$this->redirect($url);
exit(); // some people have said I need to exit the session first after I redirect, but it doesn't help at all.
}
这是我的RedirectController,这是一个单独的控制器,不在主SiteController:
public function actionIndex()
{
$type = $_GET['type'];
if ($type == "twitter")
{
$token = Yii::app()->session['token'];
print($token);
}
}
我也有会话自动启动设置为true在我的配置文件。
关于为什么它不工作的想法/我读过的东西:
- Twitter的网站是HTTPS,我是在本地主机(这不是HTTPS)。由于某些原因,我忘记了这将使会话在重定向时丢失数据。如果是这种情况,我如何在不使用HTTPS的情况下修复它?
- 当我创建新的CHttpCookies,他们也不保存,我不能检索值
- 我已经尝试过Yii::app()->user->setState代替,这也不起作用。
我找到了解决方案。它没有工作,因为我使用127.0.0.1作为重定向,而不是标准的本地主机。