我已经设法使用亚伯拉罕的twitteroauth
来实现Twitter OAuth。用户向OAuth客户端授予所需权限后,他/她将重定向回callback.php
。在我的callback.php
模板上,我有一个基本的HTML表单,其method = "POST"
。
callback.php
<pre>
<?php
/*
*Template Name: Callback
*/
?>
<?php
session_start();
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
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>
点击Submit
按钮后,会打印<script>
标记内的消息,即This works
,但在几秒钟内,会出现以下异常:
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
即使乍一看它似乎是一个图书馆问题,但我认为并非如此。我尝试使用访问令牌向Twitter API发送各种GET和POST请求,一切似乎都有效。但是,每次我嵌入一个在提交时发送POST
请求的表单时,都会引发上述异常。
这里看起来有什么问题?