在Facebook的PHP SDK中打开身份验证对话框
问题描述:
我正在整合Facebook连接在我的网站使用PHP SDK ..一切工作正常,除非我想,而不是重定向到Facebook身份验证,我想打开OAuth对话框..目前Im使用以下URL验证..它重定向至Facebook,但不开放的OAuth Dailog ..在Facebook的PHP SDK中打开身份验证对话框
https://www.facebook.com/dialog/oauth?client_id=XXXXXXXXXXX&redirect_uri=http%3A%2F%2Fwww.setsail2nz.com.au%2Fmicrosite%2Ffbalbums.php%3Fpid%3D1&state=ad89eddd7f71e7337785f604710c97e8&scope=user_photos%2Cpublish_stream%2Cmanage_friendlists%2Cemail&display=popup
任何想法?
编辑: 我知道如何做到这一点与Facebook的JS SDK ..但有没有办法做到这一点与PHP?
编辑2:好了,现在即时通讯使用JS SDK,但仍然它不是打开对话框。这里是我的代码
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXXXXXXX', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true // enables OAuth 2.0
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
function fblogin()
{
FB.login(function(response)
{
alert(response);
},{perms: "user_photos,publish_stream,manage_friendlists,email"});
}
</script>
答
要回答你的问题有关检测与PHP SDK中的对话响应:
的概率lem是getloginURL只接受一个成功或错误使用的URL参数。我需要检测用户是否在权限对话框上单击了“确定”或“取消”,以便我可以重定向到预先权限对话框页面,或显示正确的登录内容。
我所做的只是检查是否“ERROR_REASON”的网址参数中存在,如果错误是“user_denied”,并采取适当的行动。可能有更好的办法,但这对我有用。
<?php
//Facebook Authentication
$user = $facebook->getUser();
//Get Login URL
$loginUrl = $facebook->getLoginUrl();
//User is logged in
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
//User is not logged in
if (!$user) {
//Check if error_reason was generated and if the user denied login
if (isset($_REQUEST['error_reason']) && ($_REQUEST['error_reason']=='user_denied')) {
//user cancelled permissions dialog thus not logged in to app, redirect elsewhere
echo "<script type='text/javascript'>top.location.href = 'YOUR REDIRECT URL';</script>";
exit;
} else {
//user not logged in so initiate permissions dialog
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
}
//get user basic description
$userInfo = $facebook->api("/$user");
?>
您可以使用JS-SDK这一点。 – 2012-03-24 13:26:53
是的,我知道的JavaScript ..但有没有办法用PHP Sdk做到这一点? – casper123 2012-03-24 14:30:30