I have been struggling to get a facebook "post to wall" dialogue to show on my site but all it shows is code on that page.
Here is what should happen:
A visitor comes to my site and clicks a custom "Share" button I made
The visitor is then redirected them to the feed dialogue box on a different page and they share my page.
They then proceed to download what I offered.
I made a facebook app and then copied the code from the facebook developer page and filled out everything I need, but anytime I click the share it takes me to a page with all the code instead.
Well try this, load the javascript SDK:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelUrl : 'URL_TO_CHANNEL_FILE', // Channel File optional
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
// 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));
</script>
Then add a listener to your button, let's say your button has the id, bt-download
<script>
$(document).on("click", "#bt-download", function(){
var obj = {
method: 'feed',
link: 'link to your webpage',
picture: "url to the picture you want size 90px x 90px",
name: 'the name you want',
caption: 'your caption',
description: 'your description',
display: 'popup'
};
function callback(response) {
if (response && response.post_id) {
alert('Post was published.');
//here you can redirect the user to the download page
} else {
alert('Post was not published.');
}
}
FB.ui(obj, callback);
});
</script>
Related
I tried the following code for facebook share
<p>
<a rel="nofollow" target="_blank" class=" icon_facebook ">Share</a>
</p>
jQuery(document).ready(function($) {
window.fbAsyncInit = function() {
//SDK loaded, initialize it
FB.init({
appId : 'your-app-id',
xfbml : true,
version : 'v2.5'
});
};
//load the JavaScript SDK
(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'));
jQuery('.icon_facebook').click(
function(){
FB.ui(
{
method: 'share',
name: 'Facebook Dialogs',
link: 'https://developers.facebook.com/docs/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
)
});
}
);
then it showing error .
Given URL is not allowed by the Application configuration: One or more
of the given URLs is not allowed by the App's settings. It must match
the Website URL or Canvas URL, or the domain must be a subdomain of
one of the App's domains.
And the share url in both facebook all ready logined and logout condition is like https://www.facebook.com/v2.5/dialog/share?app_id=MY APP ID ,.....
But in other websites the working share page url format is different . When not login then it shows like
https://www.facebook.com/login.php?skip_api_login=1&api_key=..
when some one already login to the faccebook it shows like https://www.facebook.com/v2.3/dialog/share?redirect_uri=https%3A%2F%2Fwww.facebook.com%2Fdialog%2Freturn%2Fclose&display=popup&href=
Why my facebook share url is different ? is am missing any code part ?
Is there any mistake in my code ? please help .
From the comments:
Make sure you have the correct appId in FB init; and
The shared URL must be listed in the App Domains input under App Settings on your Dveloper console in Facebook.
I have my web application deployed in tomcat. Application loads fine with the URL:
http://domain.com/portal-app/ (domain.com=10.124.23.33:8081 i couldn't enter ip:port in url.) i have also pasted the same URL in my app settings page.
But still i get the error on console while loading the home page. I don't get js alerts too. When i click on "login" button and enter username and pwd, it just throws the same message in the facebook pop-up window. Any help on this would be much appreciated.
"Given URL is not allowed by the Application configuration.: One or more of the given URLs >>is not allowed by the App's settings. It must match the Website URL or Canvas URL, or >>>the domain must be a subdomain of one of the App's domains."
I had several times verified the URL in app settings. Both the application url that i use in browser and Facebook are same. Rather i just copy pasted the url.
Here is the code
<html>
<body>
<div id="fb-root"></div>
<script>
//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 = "./js/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
window.fbAsyncInit = function() {
FB.init({
appId : '488834651123456',
channelUrl : 'http://domain/portal-app/channel.html',
status : true,
cookie : true,
xfbml : true
});
FB.getLoginStatus(function(response) {
alert(response.status);
if (response.status === 'connected') {
alert("hi");
} else if (response.status === 'not_authorized') {
FB.login(function(response) {
if (response.authResponse) {
alert("hi");
} else {
// The person cancelled the login dialog
}
}, {scope: 'user_about_me,user_birthday,user_groups,user_hometown,user_interests,user_likes,user_location,read_stream'});
}else {
FB.login(function(response) {
if (response.authResponse) {
alert("hi");
} else {
// The person cancelled the login dialog
}
}, {scope: 'user_about_me,user_birthday,user_groups,user_hometown,user_interests,user_likes,user_location,read_stream'});
}
});
};
</script>
<fb:login-button show-faces="true" width="200" max-rows="1"></fb:login-button>
</body>
</html>
It looks like channelUrl is missing the .com and should be http://domain.com/portal-app/channel.html in your init function.
I'm trying to add Facebook Login to my website using the Facebook Javascript SDK.
Everything seems to work, but the login popup opens with annoying scrollbars when asking for permissions (I'm already logged in Facebook). I tested it with Firefox 16, IE 9. Other sites that uses the Facbook login in a popup don't have the same behavior on my machine.
Perhaps they use a different approach?
Here is my code.. it's pretty simple, and you can just copy/paste in an html page to test it. Just set your APP-ID.
<html>
<body>
<img src="images/facebooklogin.png" id="login-fb" onclick="login();" />
<script type="text/javascript">
window.fbAsyncInit = function(){
FB.init({
appId : 'APP-ID',
status : true,
cookie : true,
xfbml : false
});
};
// 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 login()
{
facebookPerms = [
'user_photos', 'email', 'offline_access', 'user_relationships', 'read_stream',
'user_about_me', 'user_birthday', 'user_education_history', //'publish_stream',
'user_hometown', 'user_interests', 'user_location', 'user_likes',
'user_religion_politics', 'user_activities', 'user_work_history'
];
//Get the facebook login status
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
ajaxLogin();
} else {
//Display the facebook login dialog
FB.login(function(response) {
if (response.authResponse) {
// user is logged in and granted some permissions.
// perms is a comma separated list of granted permissions
ajaxLogin();
} else {
// user is not logged in, display the error dialog box
errorNotLogged();
}
}, {
//Ask for permissions of Facebook
scope: facebookPerms.join(',')
});
}
});
}
</script>
</body>
</html>
I am currently writing a website and I need some help about facebook integration.
I need a function (PHP or JS, both will help) that can check if a given user shared my website,
and I couldn’t find out how to write one.
Could you please point me at the right direction?
First you have to load the Facebook SDK right after your tag:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : "YOUR APP ID",
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : false, // parse XFBML
perms : 'read_stream',
access_token : "USER ACCESS TOkEN",
frictionlessRequests : true
});
};
// 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/pt_BR/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
Then you can use the callback function to do what you want:
<script type="text/javascript">
function shareOnFacebook() {
FB.ui(
{
method : 'feed',
display : 'iframe',
name : 'name',
link : 'http://www.linktoshare.com',
picture : 'http://www.linktoshare.com/images/imagethumbnail.png',
caption : 'txt caption',
description : 'txt description',
access_token : 'user access token'
},
function(response) {
if (response && response.post_id) {
// HERE YOU CAN DO WHAT YOU NEED
alert('OK! User has published on Facebook.');
} else {
//alert('Post was not published.');
}
}
);
}
</script>
Then you should use it like this:
Share on facebook
Using the JavaScript SDK, FB.ui() and the feed dialog, you can prompt your users to share a URL on Facebook. This dialog provides a callback function so that you can tell if the post was made successfully.
Code sample lifted from the above link...
var obj = {
method: 'feed',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
name: 'Facebook Dialogs',
caption: 'Reference Documentation',
description: 'Using Dialogs to interact with users.'
};
function callback(response) {
document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
This is the easiest way to accomplish what you need.
It will not, however, allow you to test if someone shared your website's URL outside of your feed dialog.
A more complex alternative not requiring the dialog callback could be accomplished by utilizing the read_stream permission. Once you have that permission, you can scan the users past posts to see if he has ever shared your website on his wall...
Keep in mind that this will not work if the user shared your website on some one else's wall or on a page...
I make an iframe application for a fan page, the problem is that if user click on LIKE in top bar of facebook it reload application iframe page but dont refresh if user click on UNLIKE in same position
My applicatioon check if user is LIKE o NOT LIKE to the fan page and it cannot proceed if first dont click on LIKE button (the like button intend the button in top bar that out of iframe application)
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $app_id; ?>', // App ID
status : true, // check login status
channelUrl : '//www. domain .it/<?php echo $sub_dir_app; ?>/cf.php', // Channel File
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('edge.remove', function(response) {
//top.location.reload();
alert('ok NOLIKE');
});
FB.Event.subscribe('edge.create', function(response) {
//top.location.reload();
alert('ok LIKE');
});
};
// 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/it_IT/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
Use Javascript SDK with the method 'FB.Event.subscribe'. The events are:
edge.create - Like
edge.remove - Unlike
Read more:
https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/