Php TwitterOAuth:未捕获异常';此功能暂时不可用';
我正在Wordpress中开发一个应用程序,它允许用户使用他们的Twitter帐户登录,然后将用户重定向到表单。提交该表单时,会向用户的Twitter句柄发送一条推文。我正在使用Abraham的twitteroauth实现twitteroauthPhp TwitterOAuth:未捕获异常';此功能暂时不可用';,php,wordpress,twitter,oauth,twitter-oauth,Php,Wordpress,Twitter,Oauth,Twitter Oauth,我正在Wordpress中开发一个应用程序,它允许用户使用他们的Twitter帐户登录,然后将用户重定向到表单。提交该表单时,会向用户的Twitter句柄发送一条推文。我正在使用Abraham的twitteroauth实现twitteroauth <pre> <?php /* *Template Name: Callback */ ?> <?php session_start(); require "twitteroauth/autolo
<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>
成功登录Twitter后重定向模板的源代码:
然而,这就是我遇到的错误:
我试着通过打印$access\u令牌来进行调试,我确实按照预期从OAuth提供程序获得了一个唯一的令牌
我的代码似乎有什么问题,如何避免引发该异常?当oauth函数检查返回的代码是否为200时,会引发异常。
我刚刚遇到了同样的问题,现在它工作得很好:
您是否在应用程序的设置中添加了回调url?Twitter使用此设置>使用localhost:127.0.0.1/callback.php(然后在定义OAUTH_回调中使用get偶数(OAUTH_回调)
然后使用verify_credentials:)我也遇到了同样的问题,因为身份验证URL没有更新,所以每单击一次身份验证URL,访问令牌就无效
我添加了一个功能,在每次单击connect with tweeter时更新认证URL
这为我解决了这个问题,希望它能帮助其他人。这是我在学习API和通过邮递员呼叫时遇到的。您似乎只能使用特定的请求令牌对access_令牌端点进行一次调用。如果此调用失败(或确实成功),所有后续调用都将显示“此功能暂时不可用”错误
在第一次调用后,您需要通过请求\令牌重新生成访问令牌,并对端点进行授权/验证
<?php
if ( isset( $_POST['submitted'] ))
{
$response = $connection->post("statuses/update", array('status' => 'fsociety'));
}
?>
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