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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看