duanfei8149 2013-06-10 17:47
浏览 103
已采纳

用twitter登录; 使http_code为0

I have this code to enable login with twitter in my site

<?php
require("twitter/twitteroauth.php");
require 'config/twconfig.php'; //CONTAINS CONSUMER SECRET AND CONSUMER KEY
session_start();

$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);
$twitteroauth->host = "https://api.twitter.com/1.1/";
// Requesting authentication tokens, the parameter is the URL we will be redirected to
$request_token = $twitteroauth->getRequestToken('http://MY WEBSITE URL');

// Saving them into the session

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

// If everything goes well..
if ($twitteroauth->http_code == 200) {
    // Let's generate the URL and redirect
    $url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
    header('Location: ' . $url);
} else {
    // It's a bad idea to kill the script, but we've got to know when there's an error.
    die('Something wrong happened.'.$twitteroauth->http_code);
}
?>

I am using Abraham Williams twitter oauth. This worked well for a couple of weeks, but now am getting a http_code of 0, which is not even listed in twitters list of error codes. What could be the problem

  • 写回答

1条回答 默认 最新

  • dongpengqin3898 2013-06-14 19:59
    关注

    Here is snippted i am using which works fine.

    First make sure of the follownig In dev twitter, you app can read / write Refresh consumer key, Recreate access token.

    If the folling is not working, the problem lies elsewhere t is something else! see https://dev.twitter.com/search/apachesolr_search/HTTP%20CODE%200

    Please read instructions, FOLLOW THEM and YOUR CODE will WORK

    <?php
    require_once('twitteroauth.php');  
    session_start();  
    /* 
     * INSTRUCTIONS!!!
     * https://dev.twitter.com/
     * create app
     * https://dev.twitter.com/ TAB settings 
     * website: THE_URL_TO_YOUR_SCRIPT_WITH_THIS_CODE
     * callback_url http://www.YOURDOMAIN.COM/ 
     * Read, Write and Access direct messages ! 
     * Allow this application to be used to Sign in with Twitter 
     * GO BACK TO DETAILS RECREATE / REFRESH - ACCESS TOKEN! 
     */
    
    $consumerKey = '******************';
    $consumerSecret = '******************';
    $oAuthToken = '*********************';
    $oAuthSecret = '**************************';
    
    // The TwitterOAuth instance  
    $twitteroauth = new TwitterOAuth($consumerKey, $consumerSecret);  
    // Requesting authentication tokens, the parameter is the URL we will be redirected to  
    $request_token = $twitteroauth->getRequestToken('http://DOMAIN.com/YOURLOGINSCRIPT.php');  
    
    // Saving them into the session  
    $_SESSION['oauth_token'] = $request_token['oauth_token'];  
    $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];  
    
    // If everything goes well..  
    if($twitteroauth->http_code==200){  
        // Let's generate the URL and redirect  
        $url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
        header('Location: '. $url);
    } else { 
        // It's a bad idea to kill the script, but we've got to know when there's an error.  
        die('Something wrong happened.');  
    }  
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料