使用OAuth进行Twitter的问题

时间:2010-12-13 21:02:56

标签: php twitter oauth

我使用Abraham Williams php库为twitter实现了OAuth。它在个人Web服务器(Apache)上工作正常。但是当我将所有应用程序文件上传到公共Web托管域时,它就会停止工作。当我按下“使用Twitter帐户登录”按钮,指示用户使用“connect.php”建立twitter链接以验证我的应用程序时,它不会构建链接。而是控制在'connect.php'页面停止。这是connect.php

<?php
session_start();

require_once 'twitteroauth/TwitterOAuth.php';
define("CONSUMER_KEY", "***************");
define("CONSUMER_SECRET", "********************************");

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->getRequestToken('http://babar.phpnet.us/callback.php');

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

$url = $connection->getAuthorizeURL($request_token);
header('Location: ' . $url);
?>

这是callback.php

<?php
session_start();

require_once 'twitteroauth/TwitterOAuth.php';
define("CONSUMER_KEY", "****************");
define("CONSUMER_SECRET", "*********************************");

if (
    isset($_REQUEST['oauth_token']) 
    && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) 
    {
     //echo 'Session expired';
     header('Location: ./connect.php');
    }
  else {
        $connection = new TwitterOAuth(CONSUMER_KEY,CONSUMER_SECRET,
        $_SESSION['oauth_token'],$_SESSION['oauth_token_secret']);
        $_SESSION['access_token'] = 
        $connection->getAccessToken($_REQUEST['oauth_verifier']);
        header('Location: index1.php');
       }
 ?>

index1.php      

  if (empty($_SESSION['access_token'])) {
     header('Location: ./connect.php');
    }

     require_once 'twitteroauth/TwitterOAuth.php';
     define("CONSUMER_KEY", "**************");
     define("CONSUMER_SECRET", "*******************");

     $connection = new TwitterOAuth(CONSUMER_KEY,CONSUMER_SECRET,
     $_SESSION['access_token']['oauth_token'],
     $_SESSION['access_token']['oauth_token_secret']
    );

    include("index.php");
    $tweetmsg = $_POST['t_update'];
    $result = $connection->post('statuses/update', array('status' => $tweetmsg));
      if (200 === $connection->http_code) {
         //echo'tweet posted';
         }
       else {
             $resultmsg = 'Could not post Tweet. Error: '.$httpCode.'  
             Reason:'.$result->error; 
             //echo $resultmsg;
        }
     ?>

1 个答案:

答案 0 :(得分:0)

确保服务器上的时钟与NTP正确同步。如果从Twitter服务器上的时钟差异超过五分钟的时间将会失败。

您还应该检查是否有防火墙阻止https://api.twitter.com