如何处理Twitter身份验证?

时间:2014-11-09 15:49:43

标签: twitter laravel-4

我正在尝试在webapp中集成一项功能,用户点击一个按钮来发布webapp的精彩程度。 (我知道,我知道,愚蠢,但嘿,老板想要它)

我使用Thujohn的Twitter为Laravel 4设置了以下路线 - https://github.com/thujohn/twitter-l4

Route::get('/twitter/callback', function() {
    // You should set this route on your Twitter Application settings as the callback
    // https://apps.twitter.com/app/YOUR-APP-ID/settings
    if(Session::has('oauth_request_token')) {
        $request_token = array(
            'token' => Session::get('oauth_request_token'),
            'secret' => Session::get('oauth_request_token_secret'),
        );

        Twitter::set_new_config($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('/')->with('flash_error', 'We could not log you in on Twitter.');
        }

        $credentials = Twitter::query('account/verify_credentials');
        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.

            var_dump($credentials);

            //return Redirect::to('/')->with('flash_notice', "Congrats! You've successfully signed in!");
        }
        return Redirect::to('/')->with('flash_error', 'Crab! Something went wrong while signing you up!');
    }
});

但是,我真的不知道在这部分应该做些什么:

// $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.

我需要做些什么来确保用户不需要每次都进行身份验证?

1 个答案:

答案 0 :(得分:0)

如果检查$ credentials对象,则应该看到oauth访问令牌。将这些与用户数据(用户名,电子邮件,姓名等)一起保存在数据库中,下次调用twitter API时可以使用它们。