[英]Integrating Facebook JavaScript SDK in wordpress
我要实现的是,我想知道某个用户是否使用我的插件登录了Facebook。
我所做的是我遵循了facebook的指示。
在我添加的wordpress标题中的<body>
下面
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'my_app_id',
xfbml : true,
version : 'v2.7'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
我也在那里添加了我的应用程序ID
在插件中,我使用了上面有关如何识别用户的教程。 所以我将此代码添加到了插件中
<script type="text/javascript">
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
document.write('connected');
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
document.write('not_authorized');
} else {
// the user isn't logged in to Facebook.
document.write('not connected');
}
});
</script>
但是问题在于它无法正常工作。 什么都没有显示。
我做对了吗? 还是我缺少什么?
认为您应该显示Facebook按钮以登录Facebook。 例如:
<div class="fb-status text-center">
<fb:login-button data-size="xlarge" scope="public_profile,email,user_photos">
</fb:login-button>
<div id="status">
</div>
然后,您需要以某种方式知道该用户尝试使用facebook登录,然后才检查该用户是否被允许使用其姓名,电子邮件和其他选项来定义您在范围内定义的内容。
在我的插件中是这样的。 我知道这是不好的做法,但对我有用。
<script>
fbGalleries.loginStatus = false;
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
if (fbGalleries.loginStatus == false) {
setTimeout(checkLoginState, 2000);
}
else {
$('.fb-status').remove();
}
}
window.fbAsyncInit = function() {
FB.init({
appId : '*****************',
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.5' // use graph api version 2.5
});
};
// Load the SDK asynchronously
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function statusChangeCallback(response) {
if (response.status === 'connected') {
testAPI();
} else if (response.status === 'not_authorized') {
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
function testAPI() {
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
getFbPhotos(); //in my case pooling user photo galeries
fbGalleries.loginStatus = true;
});
}
</script>
一些需要运行的地方checkLoginState();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.