简体   繁体   English

在Twitter上使用OAuth的问题

[英]problem in using OAuth for Twitter

I have implemented OAuth for twitter using Abraham Williams php library. 我已经使用Abraham Williams php库为Twitter实现了OAuth。 It is working fine for me on personal web server(Apache). 我在个人Web服务器(Apache)上运行良好。 But when I uploaded all my application files to a public web hosting domain, it stops working. 但是,当我将所有应用程序文件上传到公共Web托管域时,它将停止工作。 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. 当我按下“使用Twitter帐户登录”按钮将用户引导到“ connect.php”,该按钮将构建Twitter链接以验证我的应用程序时,它不会构建该链接。 Rather control halts on that 'connect.php' page. 而是在“ connect.php”页面上停止控制。 Here is 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);
?>

Here is callback.php 这是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 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;
        }
     ?>

Make sure the clock on the server is properly synced with NTP . 确保服务器上的时钟与NTP正确同步。 If the time differers from the clocks on Twitter's servers by more then five minutes requests will fail. 如果时间与Twitter服务器上的时钟相差超过五分钟,则请求将失败。

You should also check to see if there is a firewall blocking https://api.twitter.com . 您还应该检查是否有防火墙阻止https://api.twitter.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM