Twitter 使用 OAuth 的问题

发布于 2024-10-07 18:12:33 字数 2434 浏览 0 评论 0原文

我已经使用 Abraham Williams php 库为 twitter 实现了 OAuth。它在个人网络服务器(Apache)上对我来说工作得很好。但是,当我将所有应用程序文件上传到公共网络托管域时,它停止工作。当我按下“使用 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;
        }
     ?>

I have implemented OAuth for twitter using Abraham Williams php library. It is working fine for me on personal web server(Apache). But when I uploaded all my application files to a public web hosting domain, it stops working. When I press the button 'sign in with twitter account' that directs user to 'connect.php' which builds twitter link to authenticate my application, it doesn't build the link. Rather control halts on that 'connect.php' page. Here is 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);
?>

Here is 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;
        }
     ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挥剑断情 2024-10-14 18:12:34

确保服务器上的时钟与 NTP 正确同步。如果时间与 Twitter 服务器上的时钟相差超过五分钟,请求将失败。

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

Make sure the clock on the server is properly synced with NTP. If the time differers from the clocks on Twitter's servers by more then five minutes requests will fail.

You should also check to see if there is a firewall blocking https://api.twitter.com.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文