Thujohn twitter laravel不允许序列化'Closure'

时间:2015-07-25 15:27:14

标签: php api twitter laravel-4 twitter-oauth

尝试在laravel 4中使用Thujohn Twiiter API包在laravel中实现twitter身份验证。以下错误

Error in exception handler: Serialization of 'Closure' is not allowed

继续投掷线

$token = Twitter::getRequestToken(url('twitter/callback'));

在登录功能中我无法找到为什么会发生这种情况有人帮我解决这个问题。

这是我的控制器代码:

class TwitterController extends BaseController {

    /*
    |--------------------------------------------------------------------------
    | Default Home Controller
    |--------------------------------------------------------------------------
    |
    | You may wish to use controllers instead of, or in addition to, Closure
    | based routes. That's great! Here is an example controller method to
    | get you started. To route to this controller, just add the route:
    |
    |   Route::get('/', 'HomeController@showWelcome');
    |
    */
    /*
    * user Twitter 
    */

    public function twitterlogin(){
            $sign_in_twitter = true;
            $force_login = false;

            // Make sure we make this request w/o tokens, overwrite the default values in case of login.
            Twitter::reconfig(array('token' => '', 'secret' => ''));
            $token = Twitter::getRequestToken(url('twitter/callback'));


            if (isset($token['oauth_token_secret']))
            {
                $url = Twitter::getAuthorizeURL($token, $sign_in_twitter, $force_login);

                Session::put('oauth_state', 'start');
                Session::put('oauth_request_token', $token['oauth_token']);
                Session::put('oauth_request_token_secret', $token['oauth_token_secret']);

                return Redirect::to($url);
            }

            //return Redirect::to('twitter/error');


    }

    public function twittercallback(){

        echo "test";
             if (Session::has('oauth_request_token'))
                {
                    $request_token = array(
                        'token'  => Session::get('oauth_request_token'),
                        'secret' => Session::get('oauth_request_token_secret'),
                    );

                    Twitter::reconfig($request_token);

                    $oauth_verifier = false;

                    if (Input::has('oauth_verifier'))
                    {
                        $oauth_verifier = Input::get('oauth_verifier');
                    }

                    // getAccessToken() will reset the token for you
                    $token = Twitter::getAccessToken($oauth_verifier);

                    if (!isset($token['oauth_token_secret']))
                    {
                        return Redirect::to('twitter/login')->with('flash_error', 'We could not log you in on Twitter.');
                    }

                    $credentials = Twitter::getCredentials();

                    if (is_object($credentials) && !isset($credentials->error))
                    {
                        // $credentials contains the Twitter user object with all the info about the user.
                        // Add here your own user logic, store profiles, create new users on your tables...you name it!
                        // Typically you'll want to store at least, user id, name and access tokens
                        // if you want to be able to call the API on behalf of your users.

                        // This is also the moment to log in your users if you're using Laravel's Auth class
                        // Auth::login($user) should do the trick.

                        Session::put('access_token', $token);

                        return Redirect::to('/')->with('flash_notice', 'Congrats! You\'ve successfully signed in!');
                    }

                    return Redirect::to('twitter/error')->with('flash_error', 'Crab! Something went wrong while signing you up!');
                }
        }


}

路线:

 Route::get('twitter/login','TwitterController@twitterlogin');
 Route::get('twitter/callback','TwitterController@twittercallback');
 Route::get('twitter/error','TwitterController@twittererror');

1 个答案:

答案 0 :(得分:0)

最后发现问题与路线有关。以下代码修复了它。

<强>路线:

Client tries to login -> Server check username pass etc. and sends token to client -> Client saves token in localStorage or cookie -> Sending token in request with Authorization header -> Server tokenfilter checks is token valid -> accessed restricted area

<强>控制器:

Route::get('twitter/login',array('as' => 'twitter.login','uses' => 'TwitterController@getTwitterLogin'));


Route::get('twitter/callback', array('as' => 'twitter.callback','uses' =>'TwitterController@getTwitterCallback'));

Route::get('twitter/error', array('as' => 'twitter.error','uses' =>'TwitterController@getTwitterError'));

Route::get('twitter/logout', array('as' => 'twitter.logout','uses' =>'TwitterController@getTwitterLogout'));