TwitterOAuth: Uncaught exception 'This feature is temporarily unavailable'
Asked Answered
R

3

5

I am working on an application in Wordpress which allows users to login using their Twitter accounts, and then redirects the users to a form. On submitting that form, a tweet is sent to the user's Twitter handle. I'm using Abraham's twitteroauth to implement Twitter OAuth.

The source code of the redirected template after successful Twitter login:

<pre>
<?php
/*
 *Template Name: Callback 
*/

?>  
<?php

    session_start();
    require "twitteroauth/autoload.php";
    use Abraham\TwitterOAuth\TwitterOAuth;

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

    $request_token = [];
    $request_token['oauth_token'] = $_SESSION['oauth_token'];
    $request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];

    if (isset($_REQUEST['oauth_token']) && $request_token['oauth_token'] !== $_REQUEST['oauth_token'])
    {
        echo "Opps! Something went wrong!";
    }

    else
    {
        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']);
        $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));

        //print_r($access_token);

        $_SESSION['access_token'] = $access_token;
    }

    $access_token = $_SESSION['access_token'];

    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

    //$user = $connection->get("account/verify_credentials");

    //$response = $connection->post("statuses/update", array('status' => 'fsociety'))





    //$response = $tmhOAuth->request('POST', $tmhOAuth->url('1.1/statuses/update'), array(
                                                                        //'status' => 'Conceit to fall on parasol.'


?>


<script>

    var count = 0

    function addNewMessage(count)
        {       
                if(count > 5)
                {
                    window.alert("NO MORE THAN 5!");
                }
                else
                {
                    var celeb = document.createElement("input");
                    celeb.type = "text";
                    celeb.name = "tweet" + count;
                    celeb.placeholder = "Tweet" + " " + count;
                    celebrity.appendChild(celeb);

                    var date = document.createElement("input");
                    date.type = "datetime-local";
                    date.name = "date" + count;
                    date.placeholder = "message-date" + " " + count;
                    celebrity.appendChild(date);

                    celebrity.appendChild(document.createElement("br"));
                    celebrity.appendChild(document.createElement("br"));
                }
        }        

</script>

<form method = "POST" action = "">

    <fieldset>
        <a style = "color:red" onclick = "addNewMessage(++count)">Schedule a tweet</a>
        <div id = "celebrity"/>
    </fieldset>

    <br>
    <fieldset>
        <input type="hidden" name="submitted" id="submitted" value="true" />
        <?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
        <button type="submit"><?php _e('Add Campaign', 'framework') ?></button>
    </fieldset>


</form>


<?php



if ( isset( $_POST['submitted'] ))
{
    $response = $connection->post("statuses/update", array('status' => 'fsociety'));
}

?>

</pre>

On submitting the form, I use Abraham's twitteroauth to post a tweet on the user's Twitter timeline, which I have tried to implement as follows:

<?php



if ( isset( $_POST['submitted'] ))
{
    $response = $connection->post("statuses/update", array('status' => 'fsociety'));
}

?>

However, this is the error I'm encountering:

Fatal error:  Uncaught exception 'Abraham\TwitterOAuth\TwitterOAuthException' with message 'This feature is temporarily unavailable' in /opt/lampp/htdocs/wordpress/wp-content/themes/twentyfifteen/tuto/twitteroauth/src/TwitterOAuth.php:137
Stack trace:
#0 /opt/lampp/htdocs/wordpress/wp-content/themes/twentyfifteen/tuto/callback.php(30): Abraham\TwitterOAuth\TwitterOAuth->oauth('oauth/access_to...', Array)
#1 /opt/lampp/htdocs/wordpress/wp-includes/template-loader.php(74): include('/opt/lampp/htdo...')
#2 /opt/lampp/htdocs/wordpress/wp-blog-header.php(16): require_once('/opt/lampp/htdo...')
#3 /opt/lampp/htdocs/wordpress/index.php(17): require('/opt/lampp/htdo...')
#4 {main}
  thrown in /opt/lampp/htdocs/wordpress/wp-content/themes/twentyfifteen/tuto/twitteroauth/src/TwitterOAuth.php on line 137

I tried debugging by printing the $access_token, and I'm indeed getting a unique token from the OAuth provider as expected.

What seems to be wrong with my code, and how could I avoid raising that Exception?

Roundy answered 19/8, 2015 at 12:28 Comment(0)
T
5

Just came across this as I was learning the API and making calls via Postman. It appears you can only make a single call to the access_token endpoint with a particular request token. If this call fails (or indeed succeeds), all subsequent calls will give you the "This feature is temporarily unavailable" error.

You will need to regenerate the access tokens via request_token and authorize / authenticate endpoints after the first call.

Tapping answered 15/9, 2019 at 14:59 Comment(1)
Working for me.Profitable
F
2

The exception is thrown when the oauth function check if the returned code is 200. I just encountered the same problem and now it works perfectly : Did you add a callback url on the settings of your app ? Twitter useS this setting > working with localhost : 127.0.0.1/callback.php (then use geteven(OAUTH_CALLBACK) in your define OAUTH_CALLBACK) Then use verify_credentials :)

Flemings answered 27/8, 2015 at 12:39 Comment(0)
F
2

I had the same problem because the auth URL was not renewed, so every second time that I clicked on the auth URL the access token was invalid.

I added a function that renewing the auth URL on every click on connect with tweeter.

this solved the issue for me hope it will help others.

Filbert answered 23/1, 2017 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.