I got error occurred when trying to post to wall on facebook using the below code. It sometimes can post after the first login but always return error later on.
<?php
include_once "include/settings.php";
//include_once "include/messages.php";
if(isset($_GET["access_token"]))
{
$access_token = $_GET["access_token"];
}
//$msg = $msg_en;
?>
<script language="javascript">
function publishToWall(){
var params = {};
params['message'] = 'message';
alert('<?php echo $access_token; ?>');
FB.api('/me/feed', 'post', { message: 'test' }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response);
}
});
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title></title>
<!--<link rel="stylesheet" href="css/style.css">-->
</head>
<body>
<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 : true // parse XFBML
});
publishToWall();
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
</body>
</html>
Why is your <script> tag before <html> tag? Put it inside <head> or <body>.
Related
My script below works perfectly on Chrome and Firefox but somehow doesn't work on IE. The code is to check whether the user is a fan of my page yet or not. The script neither redirect to another page (is a fan) nor show the likebox (not a fan).
Any idea? I'm very new to facebook application and javascript. Sorry for my noob.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Redirecting...please wait</title>
<script type="text/javascript" src="../../src/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../../src/js/all.js"></script>
<style type="text/css">
div#container_notlike, div#container_like {
display: none;
}
</style>
</head>
<body>
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : 'APP_ID', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
//if like botton click, reflesh page
FB.Event.subscribe('edge.create',
function(href, widget) {
//alert("Like button clicked");
window.location.reload();
});
FB.getLoginStatus(function(response) {
var page_id = "PAGE_ID";
if (response && response.authResponse) {
var user_id = response.authResponse.userID;
//var accessToken = response.authResponse.accessToken;
var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
FB.Data.query(fql_query).wait(function(rows) {
if (rows.length == 1 && rows[0].uid == user_id) {
console.log("LIKE");
//alert ("Like");
location.href="https://www.example.com/apps";
} else {
console.log("NO LIKEY");
$('#container_notlike').show();
}
});
} else {
FB.login(function(response) {
if (response && response.authResponse) {
var user_id = response.authResponse.userID;
var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
FB.Data.query(fql_query).wait(function(rows) {
if (rows.length == 1 && rows[0].uid == user_id) {
console.log("LIKE");
//alert ("Like");
location.href="https://www.example.com/apps";
} else {
console.log("NO LIKEY");
$('#container_notlike').show();
}
});
} else {
console.log("NO LIKEY");
$('#container_notlike').show();
}
}, {scope: 'user_likes'});
}
});
};
// 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/en_US/all.js#xfbml=1&appId=APP_ID";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<center>
<div id="container_notlike">
<fb:like-box href="http://www.facebook.com/example" width="292" height="62" show_faces="false" stream="false" header="false" >
</fb:like-box>
</div>
</div>
</center>
<div id="container_like">
YOU LIKE ME :)
</div>
</body>
</html>
FYI: I'm using IE9 and already update javascript to latest version. I've already opened javascript to run on IE9.
What is this?
<script type="text/javascript" src="../../src/js/all.js"></script>
If it's the FB SDK, try and remove that line.
Im trying to make wallpost from my fb app. My code (bellow) wont give eny erros but it just wont publish anything to wall. Can someone help me? =P
Application file (index.php):
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '1234567890', // App ID
channelUrl : '//WWW.YOUR_DOMAIN.COM/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
});
// Additional initialization code here
};
// 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>
Post to Facebook
<script>
function postToFacebook() {
var body = 'Reading Connect JS documentation';
FB.api('/me/feed', 'post', { body: body, message: 'My message is ...' }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response);
}
});
}
</script>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR APP ID GOES HERE',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
</body>
</html>
And channel file in server (channel.php):
<?php
$cache_expire = 60*60*24*365;
header("Pragma: public");
header("Cache-Control: max-age=".$cache_expire);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$cache_expire) . ' GMT');
?>
<script src="//connect.facebook.net/en_US/all.js"></script>
Why are You trying to init Facebook scripts twice?:)
Here's an example of a working code :
How to get FB.api('/me/feed', 'post', ... to work?
Mind You, the user needs to authorize the app first before You can make FB.api calls.
You can do that by using FB.login e.g:
http://developers.facebook.com/docs/reference/javascript/FB.login/
and check if the user actually connected to Your app by using FBgetLoginStatus():
http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
Hope this helps
at the minute I have this:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
window.fbAsyncInit = function() {
FB.init({ appId:'188492754501683', cookie:true, status:true, xfbml:true });
};
</script>
<fb:login-button perms="email">
login with Facebook
</fb:login-button>
I want to run some code to pull the email aswell, say this:
FB.api('/me', function(res) { if (res != null) { alert(res.email); } } );
When I added this under the call to FB.init(), it was run as soon as the window loaded, which is understandable but not what I wanted.
I want the code to run AFTER the user has clicked the Login With Facebook button, and then successfully logged in, etc.
Can anyone point me in the right direction, I've searched google but can't seem to find anything :/
Thanks in advance, Tom.
you can use the events for this:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Facebook Test</title>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: 'YOUR_APP_ID', status: true, cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
// user logedin
});
};
(function() {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<fb:login-button perms="email">
Login with Facebook
</fb:login-button>
</body>
</html>
see http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/ for more events you could subscribe to.
updated with full example
I have a really simple facebook IFrame app that checks whether the current user is logged into facebook. If they are not I wait 1 second then prompt them to login by opening the login dialog.
This all works perfectly when I run my app in Firefox & in Chrome. But when I run it in Internet Explorer, the Javascript function isLoggedIn() fails/breaks after I use the line
FB.getLoginStatus( function(response) {
Why do you think this happens & do you know how I can fix this? PS: I am using the Javascript SDK of the facebook Graph API.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>IFRame Test</title>
<script type="text/javascript">
function loginUser()
{
alert( "Running loginUser()" );
FB.login( function(response) {
if (response.session)
{
if (response.perms)
{
// user is logged in and granted some permissions.
// perms is a comma separated list of granted permissions
alert( "With perm" );
}
else
{
// user is logged in, but did not grant any permissions
alert( "logged in" );
}
}
else
{
// user is not logged in
alert( "Not logged in" );
}
}, {perms:'read_stream,publish_stream,offline_access'}); // these are the permissions we ask for
alert( "Ending loginUser()" );
}
function isLoggedIn()
{
alert("About to Run");
FB.getLoginStatus( function(response) {
alert( "Running isLoggedIn()" ); // ERROR HERE This line never executes in Internet Explorer
alert( response.status );
if ( response.status == "unknown" )
{
setTimeout( "loginUser()", 1000 );
}
else if ( response.status == "notConnected" )
{
// We dont have permissions but the user is logged in
setTimeout( "loginUser()", 1000 ); // ask for permsissions
}
else if ( response.status == "connected" )
{
// User is logged in & has given my app permissions
}
else { alert( "Weird case" ); }
});
}
</script>
</head>
<body>
<h1>IFRame Test</h1>
<p><fb:login-button autologoutlink="true"></fb:login-button></p>
<p><fb:like></fb:like></p>
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({appId: '172889292760575', status: true, cookie: true,
xfbml: true});
};
(function() {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
alert( "CONNECTING" );
</script>
<div>
<a href="javascript:isLoggedIn()">ABCD</p>
</div>
<script type="text/javascript">
alert( "javascriptInner" );
isLoggedIn();
</script>
</body>
</html>
Not sure if this will help, but try using:
<script src="http://connect.facebook.net/en_US/all.js"></script>
before
<div id="fb-root"></div>
Then:
FB.init({
appId : '172889292760575',
status : true,
cookie : true,
xfbml : true
});
instead of
window.fbAsyncInit = function() {
FB.init({appId: '172889292760575', status: true, cookie: true,
xfbml: true});
};
(function() {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
Because FB might be undefined by the time you try to use it
i have copied this code from the FB.init documentation:
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>
...
<!-- facebook JS SDK init -->
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init(
{
appId: '<?= FacebookHelper::FACEBOOK_APP_ID ?>',
status: true,
cookie: true,
xfbml: true
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
i placed it just before my </body> tag (as they recommend in the documentation), but i am getting this weird error (i can see via firebug):
b is undefined
[Break on this error] if(!window.FB)FB={};FB.forEach=functio...B.TypeLoader.subclass,FB.TypeLoader);
now, at first, i thought this must be a bug with my code, but the more i think about it, the more i remember hearing about some problems between firebug and facebook javascript. is that the problem, or am i forgetting something?
I think it is because you are including the older version of Facebook Connect. You don't need to include it with the newer JS SDK:
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>
Also, Facebook recommends placing the asynchronous loading code right after the opening body tag, such as (but it's your call):
<body>
<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 : true // parse XFBML
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
...
...
</body>
Firebug doesn't seem to be the issue here.
I had a similar problem with FB>UI calls for feed dialogues. My page was loaded via ajax and the UI does not poup. I just disabled the FireBug and it started to work amazingly!. Guess the FB is ambiguous between FireBug vs FaceBook for Mozilla.
Thanks all this issue is due to
<form id="fbform"></form>
<div id="fb-root"></div> is not included before script. you guys can try this code as i wrote.
div id="fb-root"></div>
<script>
//initializing API
window.fbAsyncInit = function() {
FB.init({appId: '298295206936650', status: true, cookie: true, xfbml: true});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
<script>
//your fb login function
function fblogin(action) {alert('dd');
if(action == 'undefined'){
var action = 'fb_login';
}
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
var user = response;
//alert(user.birthday)
console.log(response)
//alert(user.id+'---'+user.email);
var picture_fb='https://graph.facebook.com/'+user.id+'/picture?width=100&height=100';
// alert(picture_fb);
if (user.current_location != null) {
var city = user.current_location['city'];
var country = user.current_location['country'];
}else{
var city = '';
var country = '';
}
if(action == 'update_fb_pic'){
var email_user= '<?=$this->session->userdata('user_id');?>';
}else{
if(user.email !='' && user.email !=''){
var email_user= user.email;
}else{
var email_user= '';
}
}
$.ajax({
type: 'POST',
url: "<?php echo site_url()?>front/ajax_fb_login",
data: "action="+action+"&fb_user_id="+user.id+"&username="+user.name+"&username="+user.name+"&first_name="+user.first_name+"&last_name="+user.last_name+"&gender="+user.gender+"&email="+email_user+"&avatar="+picture_fb+"&dob="+user.birthday,
success: function(msg){
//alert(msg);
//window.parent.document.location.reload();
window.location= "<?php echo site_url()?>profile"
//$("#fbform").submit();
}
});
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, { scope: 'email,user_birthday,status_update,read_stream,publish_stream'});
}
</script>