I have managed to implement Twitter OAuth using Abraham’s twitteroauth
. After the user grants required permissions to my OAuth client, he/she is redirected back to callback.php
. On my callback.php
template, I have a basic HTML form, whose method = "POST"
.
callback.php
<pre>
<?php
/*
*Template Name: Callback
*/
?>
<?php
session_start();
require "twitteroauth/autoload.php";
use AbrahamTwitterOAuthTwitterOAuth;
define('CONSUMER_KEY', "XXXXXXXXXXXXXX");
define('CONSUMER_SECRET', "XXXXXXXXXXXXXXXXXXXX");
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'];
//print_r($_REQUEST['oauth_verifier']);
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
}
?>
<script>
function printSomething()
{
document.write("This works");
}
</script>
<form method = "post">
<input type = "submit" onclick="printSomething()">
</form>
On clicking the Submit
button, the message inside my <script>
tags ie This works
is printed, however, within seconds, the following Exception is raised:
Fatal error: Uncaught exception 'AbrahamTwitterOAuthTwitterOAuthException' 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): AbrahamTwitterOAuthTwitterOAuth->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
Even though at first glance it may seem to be a library issue, I don’t think that’s the case. I have tried sending various GET and POST requests to Twitter API using the access tokens, and everything seems to be working. However, each time I embed a form which sends a POST
request on submission, the above mentioned Exception is raised.
What seems to be wrong here?