开发者

problem in using OAuth for Twitter

开发者 https://www.devze.com 2023-01-30 14:53 出处:网络
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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号