【发布时间】:2015-05-27 18:49:17
【问题描述】:
在我转到 Twitter 的 OAuth 页面并返回重定向后,我花了一天半的时间试图弄清楚为什么 Yii 会删除所有会话数据。
这是主要的 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,我在 localhost(不是 HTTPS)上。出于某种原因,我忘记了这会使我重定向时会话丢失数据。如果是这种情况,如何在不使用 HTTPS 的情况下修复它?
- 当我创建新的 CHttpCookies 时,它们也不保存,我无法检索值
- 我已经尝试过 Yii::app()->user->setState ,但也不起作用。
【问题讨论】: