HybridAuth 不适用于 ajax
HybridAuth not working with ajax
我正在尝试使用 ajax 实现 HybridAuth。
代码:
PHP:(将被ajax调用)
<?php
header('Content-type: application/json');
$provider = $_GET["provider"];
$config = '../libaries/hybridauth/config.php';
require_once( "../libaries/hybridauth/Hybrid/Auth.php" );
try {
$hybridAuth = new Hybrid_Auth($config);
$adapter = $hybridAuth->authenticate($provider);
$userProfile = json_encode($adapter->getUserProfile());
echo $_GET['callback'] . '(' . "{$userProfile}" . ')';
} catch (Exception $e) {
echo "Ooophs, we got an error: " . $e;
}
?>
Javascript:
socialRegister: function () {
var self = this;
var val = 'provider=' + self.get("provider");
return $.ajax({
type: "GET",
url: path.urlRoot + 'ext/socialRegisterAndAuthentication.inc.php',
dataType: "jsonp",
data: val
});
}
但我总是得到以下错误:
XMLHttpRequest cannot load https://api.twitter.com/oauth/authenticate?oauth_token=123. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
我知道,这意味着 Twitter 服务器不允许我的来源。有解决方法吗? "One-Page" 可以用 ajax 注册吗? (希望是 - 在 quora.com 上有效;))
推特设置:
最佳
法比安
首先 - HybridAuth 和 ajax 直接不起作用。但我现在有一个令人满意的解决方案,我想与您分享。所以这就是我的做法:
JavaScript:
twitterRegister: function () {
var self = this;
self.popupWindow = window.socialPopupWindow = window.open(
path.urlRoot + 'ext/socialRegisterAndAUthentication.inc.php?provider=Twitter',
"hybridauth_social_sing_on",
"location=0,status=0,scrollbars=0,width=800,height=500"
);
var winTimer = setInterval(function ()
{
if (self.popupWindow.closed !== false)
{
// !== is required for compatibility with Opera
clearInterval(winTimer);
//Now twitter register from
require(["model/register"], function (registerModel) {
var registerM = new registerModel();
var ajaxRequest = registerM.socialRegister();
$.when(ajaxRequest).done(function (response) {
console.log(response);
self.slideOutRegister("Twitter");
self.twitterObject = response;
$('#reg_user').val(response.firstName);
});
$.when(ajaxRequest).fail(function () {
self.slideOutRegister("Email");
});
});
}
}, 200);
},
说明:该函数打开一个新的弹出窗口-window。系统将提示用户授权该应用程序。 setInterval 捕获关闭事件(在完成时由 window 本身触发)。
socialRegisterAndAUthentication.inc.php:
<?php
session_start();
header('Content-type: text/html');
$provider = $_GET["provider"];
$config = '../libaries/hybridauth/config.php';
require_once( "../libaries/hybridauth/Hybrid/Auth.php" );
try {
$hybridAuth = new Hybrid_Auth($config);
$adapter = $hybridAuth->authenticate($provider);
$_SESSION["userProfile"] = json_encode($adapter->getUserProfile());
echo "<script type='text/javascript'>";
echo "window.close();";
echo "</script>";
} catch (Exception $e) {
echo "Ooophs, we got an error: ";
}
?>
说明:授权完成后关闭window(这来自HybridAuth的文档)。数据存储在一个会话中,以便我以后可以根据 ajax.
检索它
getSocialData.inc.php
<?php
session_start();
header('Content-type: application/json');
echo $_GET['callback'] . '(' . "{$_SESSION["userProfile"]}" . ')';
?>
说明:Returns 存储的用户配置文件。
总结:
用 javascript 打开一个弹出窗口-window 并让用户授权该应用程序。将数据存储在 Session 变量中。捕捉弹窗的关闭事件-window。然后进行 ajax 调用以检索存储的数据(会话)。
我正在尝试使用 ajax 实现 HybridAuth。
代码:
PHP:(将被ajax调用)
<?php
header('Content-type: application/json');
$provider = $_GET["provider"];
$config = '../libaries/hybridauth/config.php';
require_once( "../libaries/hybridauth/Hybrid/Auth.php" );
try {
$hybridAuth = new Hybrid_Auth($config);
$adapter = $hybridAuth->authenticate($provider);
$userProfile = json_encode($adapter->getUserProfile());
echo $_GET['callback'] . '(' . "{$userProfile}" . ')';
} catch (Exception $e) {
echo "Ooophs, we got an error: " . $e;
}
?>
Javascript:
socialRegister: function () {
var self = this;
var val = 'provider=' + self.get("provider");
return $.ajax({
type: "GET",
url: path.urlRoot + 'ext/socialRegisterAndAuthentication.inc.php',
dataType: "jsonp",
data: val
});
}
但我总是得到以下错误:
XMLHttpRequest cannot load https://api.twitter.com/oauth/authenticate?oauth_token=123. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
我知道,这意味着 Twitter 服务器不允许我的来源。有解决方法吗? "One-Page" 可以用 ajax 注册吗? (希望是 - 在 quora.com 上有效;))
推特设置:
最佳 法比安
首先 - HybridAuth 和 ajax 直接不起作用。但我现在有一个令人满意的解决方案,我想与您分享。所以这就是我的做法:
JavaScript:
twitterRegister: function () {
var self = this;
self.popupWindow = window.socialPopupWindow = window.open(
path.urlRoot + 'ext/socialRegisterAndAUthentication.inc.php?provider=Twitter',
"hybridauth_social_sing_on",
"location=0,status=0,scrollbars=0,width=800,height=500"
);
var winTimer = setInterval(function ()
{
if (self.popupWindow.closed !== false)
{
// !== is required for compatibility with Opera
clearInterval(winTimer);
//Now twitter register from
require(["model/register"], function (registerModel) {
var registerM = new registerModel();
var ajaxRequest = registerM.socialRegister();
$.when(ajaxRequest).done(function (response) {
console.log(response);
self.slideOutRegister("Twitter");
self.twitterObject = response;
$('#reg_user').val(response.firstName);
});
$.when(ajaxRequest).fail(function () {
self.slideOutRegister("Email");
});
});
}
}, 200);
},
说明:该函数打开一个新的弹出窗口-window。系统将提示用户授权该应用程序。 setInterval 捕获关闭事件(在完成时由 window 本身触发)。
socialRegisterAndAUthentication.inc.php:
<?php
session_start();
header('Content-type: text/html');
$provider = $_GET["provider"];
$config = '../libaries/hybridauth/config.php';
require_once( "../libaries/hybridauth/Hybrid/Auth.php" );
try {
$hybridAuth = new Hybrid_Auth($config);
$adapter = $hybridAuth->authenticate($provider);
$_SESSION["userProfile"] = json_encode($adapter->getUserProfile());
echo "<script type='text/javascript'>";
echo "window.close();";
echo "</script>";
} catch (Exception $e) {
echo "Ooophs, we got an error: ";
}
?>
说明:授权完成后关闭window(这来自HybridAuth的文档)。数据存储在一个会话中,以便我以后可以根据 ajax.
检索它getSocialData.inc.php
<?php
session_start();
header('Content-type: application/json');
echo $_GET['callback'] . '(' . "{$_SESSION["userProfile"]}" . ')';
?>
说明:Returns 存储的用户配置文件。
总结:
用 javascript 打开一个弹出窗口-window 并让用户授权该应用程序。将数据存储在 Session 变量中。捕捉弹窗的关闭事件-window。然后进行 ajax 调用以检索存储的数据(会话)。