Facebook Share CallBack response Getting Failed`` - javascript

I am sharing a facebook page on my Timeline and holding a response in a callback. But each time it fails and callback response throws following error:
Can't load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and sub-domains of your app to the App Domains field in your app settings.
Here is the HTML Element on which I'm calling the method.
<a onclick="shareonfacebook()"><i class="fa fa-facebook"></i>facebook</a>
And this is the callback method for my facebook button
function shareonfacebook(){
FB.init({
appId : 'APP ID',
xfbml : true,
version : 'v2.8'
});
FB.ui({
method: 'share',
href: 'http://promo.globalhellogroup.com',
}, function(response){
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
});
}
Response is always that error message.

Related

Facebook login callback returns name but not email address

I'm using Facebook login in the simplest way, and getting a call back response:
<div id="fb-root"></div>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v6.0&appId=881911462250499&autoLogAppEvents=1"></script>
<script>
window.fbAsyncInit = function() {
FB.getLoginStatus(function(response) {
FB.api('/me', function (response) {
console.log(response);
});
});
}
</script>
I log the response to the console and receive the name and id, but not email address. How can I get the email as well? Upon researching it, it should get it by default, am I wrong?
According to the Fb docs, This is how you do it.
function statusChangeCallback(response) { // Called with the results from FB.getLoginStatus().
console.log('statusChangeCallback');
console.log(response); // The current login status of the person.
if (response.status === 'connected') { // Logged into your webpage and Facebook.
testAPI();
} else { // Not logged into your webpage or we are unable to tell.
document.getElementById('status').innerHTML = 'Please log ' +
'into this webpage.';
}
}
window.fbAsyncInit = function() {
FB.init({
appId : '{app-id}',
cookie : true, // Enable cookies to allow the server to access the session.
xfbml : true, // Parse social plugins on this webpage.
version : '{api-version}' // Use this Graph API version for this call.
});
FB.getLoginStatus(function(response) { // Called after the JS SDK has been initialized.
statusChangeCallback(response); // Returns the login status.
});
};
function testAPI() { // Testing Graph API after login. See statusChangeCallback() for when this call is made.
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
}
You can check login status in this way.
function checkLoginState() { // Called when a person is finished with the Login Button.
FB.getLoginStatus(function(response) { // See the onlogin handler
statusChangeCallback(response);
});
}
By default, fb allows only basic permissions. What you require is additional permission, hence you have to request it this way.
FB.login(function(response) {
// handle the response
}, {scope: 'email,user_likes'});
You can read more about it here. Hope it helps :)
Ok got it, I was confused and it shoudl be here:
FB.api('/me', { locale: 'tr_TR', fields: 'email,name' }, function (response) {

Share an image (as a normal post) on FB but "not as a link"

I Want to Share an image (as a normal post) on FB with some caption I've used this for sharing
<script src="//connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({ appId: '660612734042393', status: true, cookie: true, xfbml: true, oauth: true });
function postToWall() {
FB.login(function(response) {
if (response.authResponse) {
FB.ui({
method: 'feed',
picture: 'http://searchengineland.com/figz/wp-content/seloads/2015/10/google-panda-name3-ss-1920-800x450.jpg',
caption: 'Checkout the image ',
description: 'Checkout the image Checkout the image Checkout the image .'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
});
} else {
//alert('User cancelled login or did not fully authorize.');
}
}, {scope: 'publish_actions,email'});
return false;
}
<img type="button" src="../img2/shareFb.png"style="width: 109px;height: 50px;" >
When I share image using this it gets shared like this
this gets shared like a link instead I want to share this image like a normal image post like we do on FB like this
I've checked on FB sdk but didn't find the exact solution for this.
any help will be appreciated thanks

FB Automatic Wall Post

I've implemented Wall post on the loggedin user with Graph API, its working fine i want if user clicks any button on my website(for eg if he purchase any thing) this will published on his wall,below is my code but in this code it is opening Publish dialog and i want to publish automaticallly(offcourse only when he logged in with fb and already has authorized)
FB.ui(
{
method: 'feed',
name: 'Transaction succeeded',
link: 'http://abc.com/',
description: 'Success'
},
function(response) {
if (response && response.post_id) {
} else {
}
}
);
Use FB.api to post feed stories once user is authorized and granted publish_stream permission to your application:
FB.api('/me/feed', 'post', {
name: 'Transaction succeeded',
link: 'http://abc.com/',
description: 'Success'
}, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
You can publish to the Facebook graph by issuing HTTP POST requests to the appropriate connection URLs, using an access token. For example, you can post a new wall post on XYZ's wall by issuing a POST request to https://graph.facebook.com/XYZ/feed:
curl -F 'access_token=...' -F 'message=Hello, XYZ. I like this new API.'
https://graph.facebook.com/XYZ/feed
you can use curl request in .net

Javascript FB.api - undefined error

I am trying to use JS interface for facebook api and it is my first application for it.
Here is a snippet:
HTML:
<div id="fb-root"></div>
<script src="https://connect.facebook.net/ru_RU/all.js"></script>
<script type="text/javascript">facebook_init();</script>
JS:
function facebook_init() {
FB.init({
appId : '<MY APP ID IS HERE>',
channelUrl : '/media/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.api('/me', function(response) {
alert('Your name is ' + response.name);
});
}
channel.html:
<script src="https://connect.facebook.net/ru_RU/all.js"></script>
I have 'Your name is undefined' when I load this page.
But this code
FB.ui({ method: 'apprequests',
message: 'MSG',
title: 'TITLE'});
works as expected.
Could you please help me?
Thanks!
If you log your response variable via console.log(response) you'll see what's wrong:
You'll get an error object with this message:
"An active access token must be used to query information about the current user."
So if you want to get information about the current user you have to send also an Access Token. To get more information about this check the Facebook JS SDK page.
You need to make sure that the user has logged into Facebook and authorized your app, before you call FB.api('/me', ...).
Here's the general information: http://developers.facebook.com/docs/guides/web/#login
To overcome with Undefined problem use the following code:
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : '<APP ID>',
status : true,
xfbml : true
});
// Additional initialization code such as adding Event Listeners goes here
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert("connected");
connecter=true;
FB.api('/me', function(user) {
alert(user.name);
alert(user.first_name);
alert(user.last_name);
alert(user.email);
});
}
});

Fb.api post to user wall only on login

I would like to use fb.api to post on logged user, but just once. If I put this
var params = {};
params['message'] = 'gegeegeggegall! Check out www.facebook.com/trashcandyrock for more info.';
params['name'] = 'gegeggeeg - gegegege';
params['description'] = 'Check out Tegegegeg! Win merch by playing and reccomending to your friends.';
params['link'] = 'http://www.bblblba.com';
params['picture'] = 'http://summer-mourning.zoocha.com/uploads/thumb.png';
params['caption'] = 'Tgegegegeeg';
FB.api('/me/feed', 'post', params, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Published to stream - you might want to delete it now!');
}
});
It posts to users wall everytime he refreshes the site?
What to do?
What is triggering the FB.api call? If it's just code within a tag then it's going to run as soon as the browser gets to that point.
You could possibly store some sort of cookie value or something after the FB.api call then check it on page load, but that seems like more work than is probably needed.
Do you want him to post it only once, ever?
If so, you're going to need to create a "state". In order to do this, you could do it client sided (with cookies), or server sided (with a database).
Create a boolean variable named "posted", and store it in a cookie or in a database (since you're using javascript, it's probably easier to use a cookie).
var posted=getCookie("posted");
if(!posted)
{
//call the FB.api();
setCookie("posted", true, duration);
}
Definition of setCookie and getCookie: http://www.w3schools.com/JS/js_cookies.asp
You could run a FQL query and check to see if the message has already been posted by querying the stream table with your app id. Something like:
<!DOCTYPE html>
<html>
<body>
<div id="fb-root"></div>
Post To Wall
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({ appId : '**yourAppID**', status : true, cookie : true, xfbml : true });
function postToWall() {
FB.login(function(response) {
if (response.session) {
FB.api(
{
method: 'fql.query',
query: 'SELECT post_id, message from stream where app_id = **yourAppID** and source_id = me()'
},
function(response) {
if(response.length == 0){
FB.ui(
{
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
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.');
}
}
);
}
else {
alert('User already posted this message');
}
}
);
}
} , {perms:''});
}
</script>
</body>
</html>

Categories

Resources