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


#1

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)
{
$this->load->helper(‘url’);

    $this->load->spark('example-spark');

    // 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
        $consumer->callback($callback); 

        // 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
        redirect($url);
    }
    else
    {
		
        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
            $this->session->unset_userdata('oauth_token');

            // 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
        $token->verifier($verifier);

        // 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: ";
        var_dump($token).PHP_EOL.PHP_EOL;

        echo "User Info: ";
        var_dump($user);
		
		echo "Searcs: ";
        var_dump($search);
    }

Ande URL giveme the token and verifier on this way:
http://mysite.local/auth/oauth/twitter?oauth_token=zJvgaMdjN9ixc5UK3ASHQIRRdJ52yNfyNmy2X0Bk4&oauth_verifier=HZnW8VCOzBvXofeMndH5lEEkmZhftMSWm4Aadip60

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 https://api.twitter.com/oauth/access_token [ 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)

IN CONCLUSION:

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.