Saving oauth data & using it in database



I’m developing social management service, and with developing proccess I’m also learning about many technologies. Here is what I have to write:

  1. User have to authenticate with his twitter account
  2. After that, in mysql I’m saveing those parameters: oauth_token, oauth_verifier and oauth_secret
  3. In another file I’m requesting those three parameters from database, and using it on my code.

Problem is when user logout from my site (not from twitter), and than again log in, a have an error “Undefined index: oauth_token” in twitter/twitteroauth.php on line 127 (that is function “getAccessToken”). And I dont know why? I’m using Abraham’s lib and Laravel framework. Here is code:

Code for loging on twitter:


class Twitter_Controller extends OneAuth\Auth\Controller
	function action_index()

// The TwitterOAuth instance
$twitteroauth = new TwitterOAuth('cNQKBdRJm8eWIbfJ3I97Nw', 'zOe3Zuutou2UzbXo3HmilxHAxE7CkQ0bSE3G3cAt6E');
// Requesting authentication tokens, the parameter is the URL we will be redirected to
$request_token = $twitteroauth->getRequestToken('');

// Saving them into the session
$username = Cookie::get('username');
DB::table('twitter')->where('username', '=', $username)->update(array( 'oauth_token'=>$request_token['oauth_token'],
//Cookie::put('oauth_token') = $request_token['oauth_token'];
//Cookie::put('oauth_token_secret') = $request_token['oauth_token_secret'];

// If everything goes well..
    $oauthtoken = DB::table('twitter')->where('username', '=', Cookie::get('username'))->only('oauth_token');
    $url = $twitteroauth->getAuthorizeURL($oauthtoken);
    header('Location: '. $url);
} else {
    // It's a bad idea to kill the script, but we've got to know when there's an error.
    die('Something wrong happened.');


And here is "main" twitter file (for showing twitter details etc.)
<?php $oauthtoken = DB::table('twitter')->where('username', '=', Cookie::get('username'))->only('oauth_token'); $haveTwitter = DB::table('user')->where('username', '=', Cookie::get('username'))->only('have_tw'); $oauthsecret = DB::table('twitter')->where('username', '=', Cookie::get('username'))->only('oauth_secret'); $twitteroauth = new TwitterOAuth('cNQKBdRJm8eWIbfJ3I97Nw', 'zOe3Zuutou2UzbXo3HmilxHAxE7CkQ0bSE3G3cAt6E', $oauthtoken, $oauthsecret); echo $haveTwitter; if($haveTwitter == '0') { $access_token=$twitteroauth->getAccessToken(Input::get('oauth_verifier')); DB::table('twitter')->where('username', '=', Cookie::get('username'))->update(array('oauth_verifier' => Input::get('oauth_verifier'))); DB::table('user')->where('username', '=', Cookie::get('username'))->update(array('have_tw' => '1')); } if($haveTwitter == '1') { $oauth_verifier = DB::table('twitter')->where('username', '=', Cookie::get('username'))->only('oauth_verifier'); echo $oauth_verifier; //$access_token=$twitteroauth->getAccessToken($oauth_verifier); } $user_info = $twitteroauth->get('account/verify_credentials'); dd($user_info); ?>

Variables $haveTwitter is for telling script if user is first time loging in on twitter, or if he logged on twitter before.

I hope that you understand me (my english is bad) and my code. If you have some problems, please ask, I'll try to explain better

Tnx and cheers to all :)