php – TwitterOAuth:未捕获的异常’此功能暂时不可用’

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – TwitterOAuth:未捕获的异常’此功能暂时不可用’脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用wordpress中的应用程序,该应用程序允许用户使用他们的TwITter帐户登录,然后将用户重定向到表单.在提交该表单时,会向用户的Twitter句柄发送一条推文.我正在使用亚伯拉罕的twitteroauth来实现Twitter OAuth.

Twitter登录成功后重定向模板的代码

<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,$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>

在提交表单时,我使用亚伯拉罕的twitteroauth在用户的Twitter时间轴上发布推文,我试图按如下方式实现:

<?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

我通过打印$access_token尝试调试,我确实从OAuth提供程序中获取一个唯一的令牌.

我的代码似乎有什么问题,我怎么能避免提出异常?

解决方法

当oauth函数检查返回的代码是否为200时,抛出异常. 我刚遇到同样的问题,现在效果很好: 您是否在应用设置上添加了回调网址? Twitter使用此设置>使用localhost:127.0.0.1/callback.PHP(然后在定义OAUTH_CALLBACK中使用geteven(OAUTH_CALLBACK)) 然后使用verify_credentials
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

脚本宝典总结

以上是脚本宝典为你收集整理的php – TwitterOAuth:未捕获的异常’此功能暂时不可用’全部内容,希望文章能够帮你解决php – TwitterOAuth:未捕获的异常’此功能暂时不可用’所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。