Javascript 如果选中复选框,则在Facebook墙上发布消息
我想创建一个函数,在提交时在Facebook上分享一些东西,如果选中复选框,比如Ask.fm上的回复表单(我相信你现在知道我在说什么)。根据这里的一些答案,我想到了如何在点击复选框时打开Facebook授权弹出窗口,但问题是,在用户授权并向Facebook应用程序授予所有必需的权限之前,我希望保持未选中状态。更确切地说,您单击复选框,Facebook登录弹出窗口将打开,但复选框将保持未选中状态,直到您使用所有必需的权限授权应用程序。到目前为止,我有以下代码:Javascript 如果选中复选框,则在Facebook墙上发布消息,javascript,php,facebook,api,checkbox,Javascript,Php,Facebook,Api,Checkbox,我想创建一个函数,在提交时在Facebook上分享一些东西,如果选中复选框,比如Ask.fm上的回复表单(我相信你现在知道我在说什么)。根据这里的一些答案,我想到了如何在点击复选框时打开Facebook授权弹出窗口,但问题是,在用户授权并向Facebook应用程序授予所有必需的权限之前,我希望保持未选中状态。更确切地说,您单击复选框,Facebook登录弹出窗口将打开,但复选框将保持未选中状态,直到您使用所有必需的权限授权应用程序。到目前为止,我有以下代码: <script type='
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
window.fbAsyncInit = function () {
FB.init({
appId: 'APP_ID',
status: true,
cookie: true,
xfbml: true
});
};
(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));
$('#checkbox').change(function () {
if ($(this).is(':checked')) {
FB.login(function (response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
alert('User canceled login or did not fully authorize the app.');
}
}, {
scope: 'publish_stream,email',
return_scopes: true
});
}
});
function fb_publish() {
var msg = $('#message').val();
FB.ui({
method: 'stream.publish',
message: msg,
attachment: {
name: 'Name here',
caption: 'Caption here.',
description: (
'description here'
),
href: 'url here'
},
action_links: [
{ text: 'Code', href: 'action url here' }
],
user_prompt_message: 'Personal message here'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
}
});
</script>
<form onsubmit="fb_publish()">
<input type="checkbox" name="facebook" value="1" id="checkbox" />Facebook
<label for="Message">Message</label>
<input name="message" id="message" type="text" />
<input type="submit" value="post" />
</form>
//
脸谱网
消息
尝试以下代码:
<!DOCTYPE html>
<html>
<head>
<title>Facebook Login</title>
<script src="jquery-1.11.1.min.js"></script>
<script type='text/javascript'>
var perms = ['public_profile', 'email'];
var declined_perms = [];
$(window).load(function () {
window.fbAsyncInit = function () {
FB.init({
appId: 'XXXXXXXX',
status: true,
cookie: true,
xfbml: true
});
checkLoginState();
};
(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));
$('#checkbox').change(function () {
if ($(this).is(':checked')) {
if (parseFloat($(this).val())) {
customLogin();
} else {
rerequest();
}
}
});
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
testAPI();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
}
}
function checkLoginState() {
FB.getLoginStatus(function (response) {
statusChangeCallback(response);
});
}
function rerequest() {
FB.login(
function (response) {
testAPI();
},
{
scope: declined_perms.join(),
auth_type: 'rerequest'
}
);
}
function customLogin() {
FB.login(
function (response) {
testAPI();
},
{
scope: perms.join()
}
);
}
function testAPI() {
declined_perms.length = 0;
FB.api('/me/permissions', function (response) {
var responsePerms = [];
for (var i = 0; i < response.data.length; i++) {
responsePerms.push(response.data[i].permission)
if (response.data[i].status == 'declined') {
declined_perms.push(response.data[i].permission);
}
}
for (var _i = 0, _j = perms.length; _i < _j; _i++) {
if (responsePerms.indexOf(perms[_i]) < 0) {
declined_perms.push(perms[_i]);
}
}
if (declined_perms.length) {
alert('User canceled login or did not fully authorize the app.');
console.log('Please Provide access to ' + declined_perms.join());
document.getElementById('checkbox').checked = false;
document.getElementById('checkbox').value = 0;
} else {
document.getElementById('checkbox').checked = true;
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
console.log('Successful login for: ' + response.name);
});
}
});
}
function fb_publish() {
var msg = $('#message').val();
FB.ui({
method: 'stream.publish',
message: msg,
attachment: {
name: 'Name here',
caption: 'Caption here.',
description: (
'description here'
),
href: 'url here'
},
action_links: [
{ text: 'Code', href: 'action url here' }
],
user_prompt_message: 'Personal message here'
},
function (response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
}
});
</script>
</head>
<body>
<form onsubmit="return fb_publish()">
<input type="checkbox" name="facebook" value="1" id="checkbox"/>Facebook
<label for="Message">Message</label>
<input name="message" id="message" type="text"/>
<input type="submit" value="post"/>
</form>
</body>
</html>
Facebook登录
var perms=['public_profile','email'];
var\u perms=[];
$(窗口)。加载(函数(){
window.fbAsyninit=函数(){
FB.init({
appId:'XXXXXXXX',
状态:正确,
曲奇:是的,
xfbml:对
});
checkLoginState();
};
(职能(d){
var js,id='facebook jssdk',
ref=d.getElementsByTagName('script')[0];
if(d.getElementById(id)){
返回;
}
js=d.createElement('script');
js.id=id;
js.async=true;
js.src=“//connect.facebook.net/en_US/all.js”;
ref.parentNode.insertBefore(js,ref);
}(文件);
$('#复选框')。更改(函数(){
如果($(this).is(':checked')){
if(parseFloat($(this.val())){
customLogin();
}否则{
重新请求();
}
}
});
函数statusChangeCallback(响应){
log('statusChangeCallback');
控制台日志(响应);
//响应对象返回的状态字段允许
//应用程序知道此人的当前登录状态。
//响应对象的完整文档可以在文档中找到
//对于FB.getLoginStatus()。
如果(response.status===“已连接”){
//登录你的应用程序和Facebook。
testAPI();
}else if(response.status===“未授权”){
//此人已登录Facebook,但未登录您的应用程序。
}否则{
//此人未登录Facebook,因此我们不确定是否
//他们是否登录到此应用程序。
}
}
函数checkLoginState(){
FB.getLoginStatus(函数(响应){
状态更改回调(响应);
});
}
函数rerequest(){
FB.login(
功能(响应){
testAPI();
},
{
作用域:已拒绝\u perms.join(),
验证类型:“重新请求”
}
);
}
函数customLogin(){
FB.login(
功能(响应){
testAPI();
},
{
作用域:perms.join()
}
);
}
函数testAPI(){
长度=0;
api('/me/permissions',函数(响应){
var响应项=[];
对于(var i=0;i