douwen5246 2015-07-19 20:59
浏览 28

每次访问页面时,SESSION变量值都会更改

EDIT I am having this issue with chrome i have tested my script in firefox and its working fine

I am using twitterOAuth api for twitter authentication for my website but when i authorize my twitter app in return to my callback url then it is not able to verify oauth_token because $_SESSION['oauth_token'] value is changed

Here is my code index.php which generate url for authentication and store session variable

<?php

require 'autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;

define('CONSUMER_KEY', 'abc');
define('CONSUMER_SECRET', 'abc');
define('OAUTH_CALLBACK', 'http://example.tk/callback.php');

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
session_start();
$_SESSION['oauth_token'] = $request_token['oauth_token'];
echo "From Sessoin ".$_SESSION['oauth_token']."<br>";
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

$url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
echo '<a href="'.$url.'">Login with twitter</a>';
?>

Here is the code of callback url. Here user is redirected after they authorize app for authentication

<?php
session_start();
require 'autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;

define('CONSUMER_KEY', 'NxEvR3DcegC83BEKWsSqPrBpG');
define('CONSUMER_SECRET', 'hRPggFw6WNYcl8MfdOGb177y3JVwbAoSZEd2tR1HlJXq5jSRmL');
define('OAUTH_CALLBACK', 'http://www.skywebdeveloper.tk/callback.php');

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

if (isset($_REQUEST['oauth_token']) && $request_token['oauth_token'] !== $_REQUEST['oauth_token']) {
    // Abort! Something is wrong.
    echo "From Sessoin ".$_SESSION['oauth_token'];  
    echo "<br>From Request Method ".$_REQUEST['oauth_token'];


}

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

//$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));

//echo $access_token;
?>

I have commented $access_token for debugging $_SESSION variable which changes evertime. $access_token is returning error invalid token which is because its not able to verify session variable with data send back

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大