artdarek / oauth-4-laravel

OAuth Service Provider for Laravel 4
684 stars 217 forks source link

Token not found in session - Twitter error #108

Closed Fynkymynky closed 8 years ago

Fynkymynky commented 9 years ago

I'm getting this error a lot now

[2014-09-16 20:38:44] production.ERROR: exception 'OAuth\Common\Storage\Exception\TokenNotFoundException' with message 'Token not found in session, are you sure you stored it?' in /var/www/vendor/lusitanian/oauth/src/OAuth/Common/Storage/Session.php:57
Stack trace:
#0 /var/www/vendor/lusitanian/oauth/src/OAuth/OAuth1/Service/AbstractService.php(129): OAuth\Common\Storage\Session->retrieveAccessToken('Twitter')
#1 /var/www/app/models/Twitter.php(35): OAuth\OAuth1\Service\AbstractService->request('statuses/user_t...')
#2 /var/www/app/controllers/UploadsController.php(88): Twitter::main('1408965495', '1409062477')
#3 [internal function]: UploadsController->getAllData()
#4 /var/www/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(231): call_user_func_array(Array, Array)
#5 /var/www/bootstrap/compiled.php(5562): Illuminate\Routing\Controller->callAction('getAllData', Array)
#6 /var/www/bootstrap/compiled.php(5550): Illuminate\Routing\ControllerDispatcher->call(Object(UploadsController), Object(Illuminate\Routing\Route), 'getAllData')
#7 /var/www/bootstrap/compiled.php(4761): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'UploadsControll...', 'getAllData')
#8 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#9 /var/www/bootstrap/compiled.php(5106): call_user_func_array(Object(Closure), Array)
#10 /var/www/bootstrap/compiled.php(4786): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#11 /var/www/bootstrap/compiled.php(4774): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#12 /var/www/bootstrap/compiled.php(706): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#13 /var/www/bootstrap/compiled.php(687): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#14 /var/www/bootstrap/compiled.php(7440): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true)
#15 /var/www/bootstrap/compiled.php(8047): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true)
#16 /var/www/bootstrap/compiled.php(7994): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true)
#17 /var/www/bootstrap/compiled.php(10615): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true)
#18 /var/www/bootstrap/compiled.php(648): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#19 /var/www/public/index.php(49): Illuminate\Foundation\Application->run()
#20 {main} [] []
Fynkymynky commented 9 years ago

OK, think I have found the issue. I'm storing the tokens for both twitter and facebook in a database. But I've been keeping my browser open which has kept the session alive. So when I logged back into my app with the facebook button the session for twitter no longer exists. Is there a way to set the session for twitter from the values in the database? Thanks

nirmal783 commented 9 years ago

i have tried this way...

public function loginWithTwitter() {

    // get data from input
    $token = Input::get('oauth_token');
    $verify = Input::get('oauth_verifier');

    // get twitter service
    $tw = OAuth::consumer('Twitter', 'http://localhost:8000/oauth/twitter');

    // check if code is valid

    // if code is provided get user data and sign in
    if (!empty($token) && !empty($verify)) {

        try {
            if (!isset($_SESSION['token'])) {
                // This was a callback request from twitter, get the token
                $token = $tw -> requestAccessToken($token, $verify);
                $_SESSION['token'] = $token -> getAccessToken();
            }
        } catch(Exception $e) {
            return Redirect::to('oauth/user');
        }

        // Send a request with it
        $result = json_decode($tw -> request('account/verify_credentials.json'), true);

        $message = 'Your unique Twitter user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
        echo $message . "<br/>";

        SocialData::storeTwitterData($result);
        //Var_dump
        //display whole array().
        var_dump($result);
        echo "<a href='logout'>Logout</a>";
    }
    // if not ask for permission first
    else {
        // get request token
        $reqToken = $tw -> requestRequestToken();

        // get Authorization Uri sending the request token
        $url = $tw -> getAuthorizationUri(array('oauth_token' => $reqToken -> getRequestToken()));

        // return to twitter login url
        return Redirect::to((string)$url);
    }
}