我看到以前的類似線程,但沒有人解決了我的問題。
我已經把按鈕放在我的網站上,並希望在那裏執行用戶twitter登錄。推特登錄按鈕點擊使用亞伯拉罕的oauth
當我直接執行php腳本時,它會給出預期的結果,但在從html按鈕單擊時調用它時,會在螢幕控制檯中顯示「302發現錯誤」。
這裏有什麼問題?
我想在login.php中獲得echo json_encode((object) array('id' => $id, 'name' => $name));
作爲json結果。
HTML/JS代碼
<input type = "button" id = "loginTwitter" class = "btn btn-primary" value = "Login | Twitter "/>
function getTwitterVal(clb)
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
var result = xmlhttp.responseText;
alert(result);
// var obj = JSON.parse(result);
clb(obj);
}
xmlhttp.open("POST","p3.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send();
// api_call();
}
$('#loginTwitter').click(function data() {
getTwitterVal(function(obj) {
// alert(obj.name);
});
});
p3.php
<?php
/* Load required lib files. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
switch ($connection->http_code)
{
case 200:
$url = $connection->getAuthorizeURL($token);
header('Location: ' . $url);
break;
default:
echo 'Could not connect to Twitter. Refresh the page or try again later.';
}
?>
callback.php
<?php
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
$_SESSION['oauth_status'] = 'oldtoken';
header('Location: ./clearsessions.php');
}
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
$_SESSION['access_token'] = $access_token;
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);
if (200 == $connection->http_code) {
$_SESSION['status'] = 'verified';
header('Location: ./index.php');
} else {
header('Location: ./clearsessions.php');
}
的index.php
<?php
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
header('Location: ./clearsessions.php');
}
$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$content = $connection->get('account/verify_credentials');
$id = $content->{'id'};
$name = $content->{'name'};
echo json_encode((object) array('id' => $id, 'name' => $name));
?>
@ShankarDamodaran:請參閱這並不能解決我的問題。這裏我已經解釋了更多.. –
似乎'p3.php'正在發送一個302 http狀態碼。 –
@ShankarDamodaran:謝謝,我不確定。我很感激,如果你請多探索一下.. –