FB.Event.subscribe code not working - javascript

I am using this code to get the response:
<fb:like layout="button_count" notify="true" colorscheme="dark" href="http://www.fbrell.com"></fb:like>
<script>
// this will fire when any of the like widgets are "liked" by the user
FB.Event.subscribe('edge.create', function(href, widget) {
alert('You liked ' + href, widget);
});
</script>
It's not giving any response. Can anyone tell what wrong am i doing ?

Assuming that you have the FB Like button appearing, so you must be including the SDK correctly, I was in exactly the same situation - all you're missing is setting the subscribe up in the fbAsyncInit() function.
window.fbAsyncInit = function () {
FB.Event.subscribe('edge.create', function (targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
});
};
If you're doing this you don't need an application ID nor do you you need to use the FBML code, you can use the HTML5 inclusion code like <div class='fb-like' data-href='...

You're missing the inclusion of the javascript SDK files.
You're missing the FB.init
You're not running your code within the fbAsyncInit() function.
See https://developers.facebook.com/docs/reference/javascript/ for more information.

Checklist:
(Im assuming you have setup the Javascript SDK)
run the FB linter on your url, errors here may stop it working
http://developers.facebook.com/tools/debug
Include this at the top of the page
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
If your liking an app, include on your app page
<meta property="fb:app_id" content="Your app id" />
Like button, change href to your link
<fb:like layout="button_count" show_faces="false" height="21" href="http://facebook.com"></fb:like>
If its an app, FB prefers HTTPS
function init() {
FB.init({
appId: APP_ID,
status: true,
cookie: true,
xfbml: true,
oauth: true
});
//uncomment if required
//window.fbAsyncInit = function() {
FB.Event.subscribe('edge.create',
function(response) {
alert('You liked the URL: ' + response);
}
);
//}
}
init();

Related

Use the output of a javascript program on a webpage to a facebook share

I apologize I am completely new to Javascript, web dev, and facebook. I've read https://developers.facebook.com/docs/javascript/quickstart and don't know where to even start.
I have a javascript calculator and want users to be able to post their results to facebook. Example: "I got 500,000 people and 5%". Is there a way to grab the result from javascript and pipe it to a facebook post? If I have variables ageValue and PercentValue can I use them as output?
Thanks.
My calculator for reference:
http://www.iwonderifyouareoutthere.com/woman.html
The following is a simple example to share something to facebook via javascript. After getting an appid and corresponding api version you can use this for sharing/posting to Facebook
<!DOCTYPE html>
<html>
<head>
<script src="//connect.facebook.net/en_US/sdk.js"></script>
</head>
<body>
<p id="demo">Facebook Share</p>
<button onclick="myFunction()">Share</button>
<script>
FB.init({
appId: appid,
status: true,
cookie: true,
xfbml: true,
version: version
});
function myFunction(){
FB.ui({
method: 'feed',
name: 'hi',
link: 'www.google.com',
picture: 'pictureurl',
caption: 'any caption ',
description:'any description'
});
}
</script>
</body>
</html>

How to get posts from facebook page using javascript or php

I want post from my facebook page, can anyone please tell me the steps to do it? I see the solutions in web but can't getting what to do, can anyone help me to do this? I just have this code.
<html>
<script src="https://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<body>
<p>Log In with the JavaScript SDK</p>
<script>
logInWithFacebook = function() {
FB.login(function(response) {
if (response.authResponse) {
alert('You are logged in & cookie set!');
// Now you can redirect the user or do an AJAX request to
// a PHP script that grabs the signed request from the cookie.
} else {
alert('User cancelled login or did not fully authorize.');
}
});
return false;
};
window.fbAsyncInit = function() {
FB.init({
appId: 'my-app-key',
cookie: true, // This is important, it's not enabled by default
version: 'v2.2'
});
};
</script>
</body>
</html>
In your application you have inserted wrong domain details. Check your settings.
Select the correct App and click in the edit button;
Check the URLs & paths are correctly entered and are pointing to the site where you have installed Ultimate Facebook plugin.

Uncaught ReferenceError: FB is not defined

I know this question is asked frequently, but I have not found the solution.
I use this code:
open the html tag with
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.me/ns/fb#">
in the index immediately after the opening tag I entered this
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : 'MY_APP_ID', // App ID from the app dashboard
status : true, // Check Facebook Login status
xfbml : true // Look for social plugins on the page
});
// Additional initialization code such as adding Event Listeners goes here
};
// Load the SDK asynchronously
(function(){
// If we've already installed the SDK, we're done
if (document.getElementById('facebook-jssdk')) {return;}
// Get the first script element, which we'll use to find the parent node
var firstScriptElement = document.getElementsByTagName('script')[0];
// Create a new script element and set its id
var facebookJS = document.createElement('script');
facebookJS.id = 'facebook-jssdk';
// Set the new script's source to the source of the Facebook JS SDK
facebookJS.src = '//connect.facebook.net/it_IT/all.js';
// Insert the Facebook JS SDK into the DOM
firstScriptElement.parentNode.insertBefore(facebookJS, firstScriptElement);
}());
</script>
finally, the button
<fb:like href="http://mydomain.it/" layout="button_count" action="like" show_faces="false" share="false"></fb:like>
<script>
FB.Event.subscribe('edge.create',
function(href, widget) {
alert('Ti piace ' + href);
});
</script>
the like works, but does not open the alert and I got this error in console.
Uncaught ReferenceError: FB is not defined
Any solutions?
See this thread: FB is not defined in Facebook Login.
You are trying to use FB right after your button, and it's not defined yet:
<fb:like href="http://mydomain.it/" layout="button_count" action="like" show_faces="false" share="false"></fb:like>
<script>
FB.Event.subscribe(...
^--- FB isn't defined yet!
Move that FB.Event.subscribe code inside your window.fbAsyncInit function, where you have that comment "Additional initialization code"
Another reason why you get this error is because you are missing ; after the fbAsyncInit definition bracket.
window.fbAsyncInit = function() {
};

FB.Canvas.setUrlHandler

Are someone succed to use the method FB.Canvas.setUrlHandler please?
I've already read the following URL's
http://developers.facebook.com/docs/reference/javascript/FB.Canvas.setUrlHandler
http://​developers.facebook.com/blog/​post/555
but it still don't work... Someone to help me please?
Thanks in advance.
Here is my source code , when I execute this, nothing gets logged in the console:
<html>
<head>
</head>
<body >
<div id='fb-root'> </div>
<script type="text/javascript" src="http://?connect.facebook.net/en_US/?all.js"> </script>
<script type="text/javascript">
FB.init({
appId : 'MY_APP_ID',
oauth : true,
status : true,
cookie : true,
oauth : true,
xfbml: true
});
function testUrlHandler(data) {
if (data.path.indexOf("test1"?) != -1)
console.log('test1');
else if (data.path.indexOf("test2"?) != -1)
console.log('test2');
else
console.log('default');
}
FB.Canvas.setUrlHandler(te?stUrlHandler);
</script>
</body>
</html>
Per the documentation, the callback is only for links on the parent iframe (Facebook UI)
Registers the callback for inline processing (i.e. without page
reload) of user actions when they click on any link to the current app
from Canvas, including:
All ticker stories
Bookmarks
Requests from the bookmarks drop-down list
Request Notifications stories.
Your links in app wont fire the event.
I've spend 3 hours trying to understand what's wrong with it. Sometimes (really rare occasions ) this thing works just fine, and sometimes FB api just ignores this callback.
Solution - set the callback with delay after init, solution: How to work with facebook setUrlHandler?

How can I detect that a user signed into Facebook?

Here is my scenario. When a user opens a browser and goes to facebook.com to sign himself in, I want to be able to detect that and init my Facebook application sign-in process.
Is that possible? I'm asking because I noticed that signing into Facebook itself doesn't get my app signed in automatically, which is not good.
I'd prefer JavaScript client code.
UPDATE: Here you have a FULLY WORKING example. The only thing you will need to do is to replace the APP_ID for whatever ID you get when sign your app at: http://www.facebook.com/developers/ and the port you may use for the localhost test. This detects when the user logs in even in another browser window. It is obviously extremely basic but proves the point that is feasible with the tools from the graph API.
You can add this code to detect when the user logs in. It is the same oauth log in whether the user clicks on a <fb:login-button> or logs directly onto facebook.com:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Hello App Engine</title>
<SCRIPT type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></SCRIPT>
</head>
<body>
<h1>Hello!</h1>
<div id="fb-root"></div>
<script type="text/javascript">
FB.init({
appId : APP_ID,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
/*
* Authentication functions
*/
FB.Event.subscribe('auth.login', function (response) {
welcomeMsg(response);
});
FB.Event.subscribe('auth.logout', function (response) {
alert ("Good bye!");
});
if (FB.getSession() != null) {
FB.api('/me', function(response){
console.log(response.name);
welcomeMsg(response);
})
} else {
window.setTimeout(function(){window.location.href="http://localhost:8888"},5000);
}
function welcomeMsg(userData) {
console.log ("Welcome " + userData.name);
}
</script>
</body>
</html>
You can subscribe to the auth.login event.
http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/
There are many Facebook images that you can see only if you're logged into Facebook. You can make the user attempt to load one of these images, and then detect if it loaded or not. It's a total trick.

Categories

Resources