Twitter OAuth: There is no request token for this page

I’m using Abraham’s TwitterOAuth library to implement Twitter OAuth in my application. However, on clicking the Login button, users are sometimes redirected to the following page:

enter image description here

Read More

I said ‘sometimes’, because sometimes the Twitter OAuth provider does generate the request token, and the users are taken to the ‘Grant Permission’ page.

Is this a library issue? Or is this an issue with the Twitter OAuth provider? If there was an issue with my code, then this page should appear every time a user tries to login using his/her Twitter account, and not at random tries.

Here’s the code of the template that the users are redirected to after clicking the Login button:

<?php

/*
 *Template Name: OAuth

*/

?>

<pre>

<?php

    session_start();
    require "twitteroauth/autoload.php";
    use AbrahamTwitterOAuthTwitterOAuth;

    define('CONSUMER_KEY', "XXXXXXXXXXXXXXX");
    define('CONSUMER_SECRET', "XXXXXXXXXXXXXXXXXXXX");
    define('OAUTH_CALLBACK', "http://localhost/wordpress/index.php/callback/");

    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
    $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
    $_SESSION['oauth_token'] = $request_token['oauth_token'];
    $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

    $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));

    header('Location: '.$url);
?>

</pre>

PS: I also tried regenerating the Consumer Key and Consumer Secret, but that doesn’t seem to have solved the problem.

Related posts

1 comment

  1. The two scenarios that seem most likely to me are:

    1) There is an error while getting the request token. Try adding some error handling.

    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
    $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
    if ($connection->getLastHttpCode() == 200) {
        $_SESSION['oauth_token'] = $request_token['oauth_token'];
        $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
        $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
        header('Location: '.$url);
    } else {
        var_dump($request_token);
        exit('Error getting request_token');
    }
    

    2) Twitter has a bug where it’s not recognizing the the request_token for some reason.

    The next step in debugging is to find out the status of $request_token that results in the error.

Comments are closed.