Post on Facebook wall with Javascript sdk - facebook

So, I'm trying to post on users wall using fb.api and I'm stuck.
Here is my code:
<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=windows-1251">
<title>Example Of Posting To Wall Using Javascript Graph API</title>
<script type="text/javascript" src=""></script>
</head>
<body class="">
//the facebook sdk include
<div id="fb-root" class=" fb_reset">
<script>
var APP_ID="myAppID";
window.fbAsyncInit = initFacebook;
function initFacebook()
{
FB.init({
appId : APP_ID,
status : true, // check login status
cookie : false, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.getLoginStatus(onFacebookLoginStatus);
};
(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);
}());
//the login function
function facebookLogin()
{
var loginUrl="http://www.facebook.com/dialog/oauth/?"+
"scope=publish_stream&"+
"client_id="+APP_ID+"&"+
"redirect_uri="+document.location.href+"&"+
"response_type=token";
window.location=loginUrl;
}
//Callback function for FB.login
function onFacebookLoginStatus(response)
{
if (response.status=="connected" && response.authResponse)
{
document.getElementById("txtEcho").innerHTML="Logged in.";
}
else
{
document.getElementById("txtEcho").innerHTML="Not logged in.";
}
}
//post to wall function
function postToWallUsingFBApi()
{
var data=
{
caption: 'This is my wall post example',
message: 'Posted using FB.api',
link: 'http://wwww.permadi.com/blog/',
}
FB.api('/me/feed', 'post', data, onPostToWallCompleted);
}
//the return function after posting to wall
function onPostToWallCompleted(response)
{
if (response)
{
if (response.error)
{
document.getElementById("txtEcho").innerHTML=response.error.message;
}
else
{
if (response.id)
document.getElementById("txtEcho").innerHTML="Posted as post_id "+response.id;
else if (response.post_id)
document.getElementById("txtEcho").innerHTML="Posted as post_id "+response.post_id;
else
document.getElementById("txtEcho").innerHTML="Unknown Error";
}
}
}
</script>
<input id="loginButton" type="button" value="Login To Facebook" onclick="javascript:facebookLogin();">
<input id="postToWallWithFBApiPrompt" type="button" value="Post To Wall Using FB.api" onclick="javascript:postToWallUsingFBApi();">
<div id="txtEcho"><b></b></div>
</body>
</html>
The problem here is that I receive this error code: An active access token must be used to query information about the current user.
I get that even if I use the log in button to get the code. Is there a possibility to add previously obtained access token in function postToWallUsingFBApi(). And can I change the /me/ with user id, so that way the user can be logged out and still post?

If you are doing client-side login that way, you have to extract the access token from the URL yourself afterwards – see https://developers.facebook.com/docs/howtos/login/client-side-without-js-sdk/#step3
Or you just use the FB.login method out of the JS SDK – a little easier, handles all the ecessary stuff “out of the box” – https://developers.facebook.com/docs/howtos/login/getting-started/#step4

Related

how to implement facebook like on a page with test that user have liked it or not

<div class="like-button">
<div class="fb-like-box" data-href="https://www.facebook.com/pages/Annupurnas-Cooking/526672940684211?ref=hl" data-width="292" data-show-faces="true" data-stream="true" data-header="true"></div>
<div id="fb-root"></div>
<script type="text/javascript">
<!--
var fbAsyncInit = function() {
var APP_ID = '324859924285629';
var PAGE_ID = '526672940684211';
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
});
FB.Event.subscribe('auth.sessionChange', function(response) {
if(response.session){
//check to see if user is a fan of the page
var query = FB.Data.query( 'select page_id from page_fan where uid='+response.session.uid+' and page_id ='+PAGE_ID);
query.wait( function(rows) {
if(rows.length){
//user already likes your page
}else{
//user has not yet liked your page
}
});
}else{
//user has not yet logged in
}
});
FB.Event.subscribe('edge.create', function(response) {
//user just clicked "like" on your page
});
FB.Event.subscribe('edge.remove', function(response) {
//user just clicked "unlike" on your page
});
};
(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>
</div>
I want to implement the like button of my facebook page. I want to check that current user have liked us or not. can someone tell me how we can do this.
in the code I put my appId and facebook page ID. I tried to use it but it's told me in error that "An active access token must be used to query information about the current user.".
do someone know how I can put access-token to this. Do I need to put accesstoken for like kind of functionality.
Check here:
API -> User
You can check if a User likes a specific page by issuing an HTTP GET to /PROFILE_ID/likes/PAGE_ID. This requires the user_likes (current user) or friends_likes (current user's friend) permission.
When you have user_likes permission you can easily check against graph api:
https://graph.facebook.com/me/likes/PAGE_ID&access_token=xxxxxxxxx

How Can I Capture an Event When I Login / Logout from Facebook Comment Box?

Is there any way to capture a Facebook event when I login or logout from any Facebook social plugin, like comment box or like button? I want to refresh my page whenever a user logins into a Facebook plugin. Because when I login with comment box it doesn't show that I am logged in and so I can't use the "Like" button. But when I refresh my page it shows I am logged in to both plugins.
Here is my code:
<div id="fb-root">
</div>
<script>
window.fbAsyncInit = function () {
FB.init({ appId: 'My App ID', status: true, cookie: true, xfbml: false });
setTimeout(aaa, 6000);
FB.Event.subscribe("xfbml.render", function (targetUrl) {
$("#myh1").html("Facebook Loaded");
alert('edge.create');
});
FB.Event.subscribe('auth.authResponseChange', function (response) {
alert('The status of the session is: ' + response.status);
});
};
(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);
} ());
function aaa() {
FB.XFBML.parse(document.getElementById('fbDiv'), function () {
alert('I rendered');
});
}
</script>
<div id="fbDiv">
<div class="fb-comments" data-href="http://www.facebook.com/AiLiveCaptions" data-num-posts="2" data-width="470">
</div>
</div>
the way i see it, this could be the default behaviour when you are using two separate plugins weather they are of facebook or any third party website i.e.facepile/commentbox.
your browser needs to reload in order to update the session on page across multiple plugins on same page, just like the difference when you are open any mail id across multiple tabs with opening same id on different browsers.

FB is not defined in Facebook Login

I have the following code but the FB.login gives a FB is not defined javascript error.
What am I missing?
<html>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function()
{
FB.init
({
appId : 'XXXX',
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);
}());
FB.login(function(response)
{
if(response.session)
{
if(response.perms)
{
alert('yippee');
}
else
{
alert('oh no');
}
}
else
{
alert('login silly');
}
}, {perms:'email,user_birthday'});
</script>
hello
<fb:login-button></fb:login>
</body>
</html>
The function assigned to window.fbAsyncInit is run as soon as the SDK is loaded. Any code that you want to run after the SDK is loaded should be placed within this function and after the call to FB.init. For example, this is where you would test the logged in status of the user or subscribe to any Facebook events in which your application is interested.
Try by keeping other initialization code in between inialization and login function
Or keep FB.login code in $(document).ready
$(document).ready(function(){
FB.login(function(response)
{
if(response.session)
{
if(response.perms)
{
alert('yippee');
}
else
{
alert('oh no');
}
}
else
{
alert('login silly');
}
}, {perms:'email,user_birthday'});
});
You should not call to FB.login before JS-SDK is loaded, window.fbAsyncInit designed especially for this. If you move this call to be within window.fbAsyncInit function you'll be fine but be aware of fact that calling to FB.login opens popup, doing this without user interaction will be probably blocked by most browsers. If you want to use FB.login to handle login, you must do it on click or sumbit events...
BTW, you already have fb:login-button which is doing login once user clicks on it.
You can also get this error if you try to launch the facebook example code on your machine (e.g. copying the code in a .html file and trying to open it in your browser). You will need to upload this to your server and run it from there.

LogOut from Facebook using childBrowser and PhoneGap in IPhone

I'm working on PhoneGap and have a FBConnect. Using childBrowser and the blog'http://www.pushittolive.com/post/1239874936/facebook-login-on-iphone-phonegap', I have logged in to the App. But could not logout from the app. It's autosigned in for each time I login.
Can any one tell me how to logout from the FBConnect using Childbrowsern PhoneGap?
Try this.
Add this function to ChildBrowserCommand.m
-(void) deleteCookies:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *each in [cookieStorage cookies]) {
[cookieStorage deleteCookie:each];
}
}
Add this to Childbrowser.js
ChildBrowser.prototype.LogOut = function()
{
PhoneGap.exec("ChildBrowserCommand.deleteCookies");
}
Add this to FBConnect.js
FBConnect.prototype.Logout = function()
{
window.plugins.childBrowser.LogOut();
}
At your page add this code to logout button on click
function Logout()
{
var fb=FBConnect.install();
fb.Logout();
}
Enjoy.
Best way to tell facebook to logout.
It can Be done by opening below url in child browser:
"https://www.facebook.com/logout.php?next="+your_site_url_registered _on_fb+"&access_token="+accessToken
I have struggled with this problem for ages. You may be logged on facebook but you may not have the access token so the conventional logot won't work.
By the way here's how to force the facebook logout without access token.
<!DOCTYPE html>
<html xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Force facebook logout without access token</title>
</head>
<body>
<h3>Please wait while we redirect kick you out from Facebook...</h3>
<div id="status"></div>
<div id="fb-root"></div>
<script type="text/javascript">
function spitInfo(message) {
var infoDiv = document.getElementById("status");
infoDiv.innerHTML += message;
infoDiv.innerHTML += "<br/>";
}
window.fbAsyncInit = function () {
spitInfo("called asyncInit");
FB.init({
appId: '496676837086475', //change the appId to your appId
status: true,
cookie: true,
xfbml: true,
oauth: true
});
function forceLogOut(response) {
if (response.authResponse) {
FB.logout(function (response) {
spitInfo("FB.logout caller - you are logged out");
});
} else {
spitInfo("Already logged out");
}
}
// run once with current status and whenever the status changes
FB.getLoginStatus(forceLogOut);
FB.Event.subscribe('auth.statusChange', forceLogOut);
};
(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>
</body>
</html>

Facebook Graph API: FB.login() called before calling FB.init()

I'm trying to use the new Facebook Graph API on my website. This is what I have:
Somewhere on the page:
<fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"></fb:login-button>
Right after the tag:
<div id="fb-root">
</div>
<script type="text/javascript">
window.fbAsyncInit = function () {
FB.init({ appId: '<%= ConfigurationManager.AppSettings["FBAppId"] %>', status: true, cookie: true, xfbml: true });
/* All the events registered */
FB.Event.subscribe('auth.login', function (response) {
// do something with response
alert("login success");
});
FB.Event.subscribe('auth.logout', function (response) {
// do something with response
alert("logout success");
});
FB.getLoginStatus(function (response) {
if (response.session) {
// logged in and connected user, someone you know
alert("login success");
}
});
};
(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>
but when I click on the generated login button, nothing happens.
In addition, I'm getting
FB.getLoginStatus() called before calling FB.init().
in Firebug console.
Any ideas?
I can't believe it, I was referencing a non existing key in the Web.config hence FB.init was failing silently.
It works as expected now.
To be clearer I wasn't passing the appId to FB.init, once I did, it worked.
Had the same issue, here is the solution that worked for me. Just put the following in your head section or in other words, add the app_id to the source of the facebook js.
<script src="//connect.facebook.net/en_US/all.js&appId=xxxxxxxxxxxxxxx" type="text/javascript"></script>
In my case, the problem went away after I disabled the XFBML functionality (i.e. setting the xfbml key to false, removing the fb: namespace and the #xfbml=1 fragment in <script src="…">). Good that I didn’t need it anyway.
YMMV, there are several other valid reasons why this error message can appear.