Problem with token, when is back to my page. I recive an invalid / expired token Error 401


HI all,

Sorry for my english is not very good, I have a problem with oauth autentication with Twitter API,

  1. The first time that I call my code work perfectly, go to the twitter autorize and send me back to the callback url and work perfectly

this is my code with PHP and CodeIgniter:

public function oauth($provider)


    // Create an consumer from the config
    $consumer = $this->oauth->consumer(array(
        'key' => 'api_key',
        'secret' => 'api_secret',

    // Load the provider
    $provider = $this->oauth->provider($provider);

    // Create the URL to return the user to
    $callback = site_url('auth/oauth/'.$provider->name);

    if ( ! $this->input->get_post('oauth_token'))
        // Add the callback URL to the consumer

        // Get a request token for the consumer
        $token = $provider->request_token($consumer);

        // Store the token
        $this->session->set_userdata('oauth_token', base64_encode(serialize($token)));

        // Get the URL to the twitter login page
        $url = $provider->authorize($token, array(
            'oauth_callback' => $callback,

        // Send the user off to login
        if ($this->session->userdata('oauth_token'))
            // Get the token from storage
            $token = unserialize(base64_decode($this->session->userdata('oauth_token')));
			echo $token;


        if ( ! empty($token) AND $token->access_token !== $this->input->get_post('oauth_token'))
            // Delete the token, it is not valid

            // Send the user back to the beginning
            exit('invalid token after coming back to site');

        // Get the verifier
        $verifier = $this->input->get_post('oauth_verifier');
		echo $verifier;
        // Store the verifier in the token

        // Exchange the request token for an access token
        $token = $provider->access_token($consumer, $token);

        // We got the token, let's get some user data
        $user = $provider->get_user_info($consumer, $token);

        // Here you should use this information to A) look for a user B) help a new user sign up with existing data.
        // If you store it all in a cookie and redirect to a registration page this is crazy-simple.
    $search = $provider->get_search($consumer, $token,'test');
        echo "<pre>Tokens: ";

        echo "User Info: ";
		echo "Searcs: ";

Ande URL giveme the token and verifier on this way:

And my problem is that when I refresh the page or I try to get other think of twitter api, the token has expired or invalid…

This is my Error:

Fatal error: Uncaught exception ‘Exception’ with message 'Error fetching remote [ status 401 ] <?xml version="1.0" encoding="UTF-8"?> Invalid / expired Token /oauth/access_token OAuth::remote(‘https://api.twi…’, Array) #1 OAuth_Request_Access->execute() #3 OAuth_Provider->access_token(Object(OAuth_Consumer), Object(OAuth_Token_Request)) #4 [internal function]: Auth->oauth(‘twitter’) #5 call_user_func_array(Array, Array)


The problem is the token is expired or something like that… I need to work with api without redirections with each call to the api… :frowning:

Thanks in Advance.