问题
I want to catch the response when a share is made on Facebook and on the response an alert will occur.
I made the following script
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId:'<?php echo $this->config->item('appID'); ?>', cookie:true,
status:true, xfbml:true,oauth : 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));
function get_fb_share() {
FB.ui({
method: 'feed',
name: 'IGUTS Share',
link: "<?php echo base_url();?>",
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.',
message: 'Facebook Dialogs are easy!'
}, function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
});
}
</script>
I put the above code on header.
Then I am calling the get_fb_share()
by the following manner:
<div class="fb-share-button" data-href="<?php echo base_url();?>"
data-width="50" data-type="button_count"
onclick="get_fb_share();"></div>
Now I don't know what I made wrong; I can share the link, but I can't get any FB response.
Can anybody tell me why?
Edit
What I found out is that when I am using this div for the sharing i.e.
<div class="fb-share-button" data-href="<?php echo base_url();?>"
data-width="50" data-type="button_count"
onclick="get_fb_share();"></div>
I am not getting any response.
But if I use a normal button like this, i.e.
<input type="button" onclick="get_fb_share();" value="share"/>
Then I am getting the response. But this is not perfect. I am using the Facebook
<div class="fb-share-button">
Because it shows the no. of shares as well. That's why I need the get_fb_share() function be called by the Facebook sharing div and not just by any button.
回答1:
I´ve answered this in a newer thread, but i will just copy my answer here:
The feed dialog is deprecated afaik, you should use the share dialog instead. It´s a lot easier to handle:
FB.ui({
method: 'share',
href: your-url
}, function (response) {
console.log(response);
});
This is what i get in the console when i cancel the share:
Object {error_code: 4201, error_message: "User canceled the Dialog flow"}
...and this is what i get when i share:
[]
Yes, that´s an empty array, and it will hit the "Post not published" alert. The post id will only be available if you authorize the user with publish_actions
, as you can read in the docs.
This worked for me:
FB.ui({
method: 'share',
href: your-url
}, function (response) {
if (response.error_code) {
console.log(response);
} else {
console.log('published');
}
});
BUT: You should not use those callbacks imho, incentivizing shares is not allowed and the user should be able to share more often if he wants to.
来源:https://stackoverflow.com/questions/23715680/no-response-back-on-facebook-sharing