Publish to Current user's wall using FBJS in FBML application - facebook

I need to publish some message to the current user's wall using FBJS in FBML application.
When I use
window.fbAsyncInit = function() {
FB.init({appId: 'MY_APP_ID', status: true, cookie: true, xfbml: false});
};
i am getting an error :- FB is not defined.
and
window is not defined.
For publishing i am using this code
function graphStreamPublish(){
var body = document.getElementById("txtTextToPublish").value;
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
// alert('Error occured');
} else {
// alert('Post ID: ' + response.id);
}
});
}
(I cannot use alert in facebook..)
Thanks in advance..

If you're using FBML, you should be using FBJS instead of the Facebook Connect API.
var body = document.getElementById('txtTextToPublish').getValue();
Facebook.streamPublish(body);
Note the getValue() call instead of .value due to use of FBJS.

Related

Facebook Post through Graph API with access token

I am developing a facebook app where I need impersonation of users, so there is an option when you use facebook for "Use Facebook As", i need to implement the same in my app.
I see the access tokens retrieved for the pages I own, but I don't understand how to use this with the following graph api to post
var body = 'Reading JS SDK documentation';
FB.api('/me/feed', 'post', { message: body }, function (response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
so my question is how do I set the access token for the api in this case??
I Hope, am clear with my question
Add it with the parameters. Change this line
FB.api('/me/feed', 'post', { message: body }, function (response) {
to...
var my_token = "the access token value";
FB.api('/me/feed', 'post', { message: body, access_token : my_token }, function (response) {

Publishing to the Facebook Graph API returns a strange response object

I'm using the Facebook JS SDK to publish to the GRAPH API, and while my post publishes successfully, it returns a strange response object that gets caught by the jQuery .error chain. Here's my code:
var jqxhr = $.post("https://graph.facebook.com/"+sbFacebook.authentication.userID+"/feed",
{
'message' : $("#question-text").val(),
'access_token' : sbFacebook.authentication.accessToken
}).success(
function(response) {
console.log(response);
})
.error(function(response) {
alert(response);
})
The response object looks like this:
http://img689.imageshack.us/img689/964/screenshotfrom201302222.png
Anyone have any idea what's going on and why it's getting caught by the error() function?
Thanks.
This is not how you should use Facebook JS SDK.
var body = 'Reading JS SDK documentation';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
Don't forget to initialize the JS SDK:

Facebook Feed Dialog does not open

I am building a custom multi friend selector in order to send posts to friend's walls on Facebook based on this tutorial
https://developers.facebook.com/docs/guides/games/custom-muti-friend-selector/
I have been able to load my list of friends and create a CSV list of people I want to post to but the last step of opening the feed dialog is failing and I don't understand why. The dialog does not open.
this is my sendRequest function
var sendUIDs = '';
$('.cbFriendId').each(function(index){
if($(this).prop('checked') == true){
var addId = $(this).attr('value');
sendUIDs += addId + ',';
}
});
console.log(sendUIDs);
openSendDialog(sendUIDs);
}
function openSendDialog(sendUIDs){
console.log('open send dialog');
FB.ui({
method: 'feed',
to: sendUIDs,
link: 'http://www.mydomain.com',
display: 'iframe',
message: 'Hello from My Domain',
name: 'Click to view my app',
caption: 'Hello my app'
},
function (response) {
if (response) {
alert('Post was published.');
console.log('Post was published.');
} else {
alert('Post was not published.');
console.log('Post was not published.');
}
}
);
}
The last console log entry is 'open send dialog'. I get no errors in IE9 dev tools but I do get an 'Unsafe Javascript attempt to access frame ...' error in chrome.
just for sanity sake here is my graph init
window.fbAsyncInit = function () {
FB.init({
appId: '462750593765445', // App ID
channelUrl: '//www.mydomain.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
frictionlessRequests: false
});
};
Also I should mention that the app is in sandbox mode.
Thanks for the help
What you're trying to do is called Send Dialog, So you need to change your method from feed to send and it should work.
Check out their docs for more information: https://developers.facebook.com/docs/reference/dialogs/send/

How to call FB.api outside window.fbAsyncInit

so i have fb api initialized
window.fbAsyncInit = function() {
FB.init({
appId : '351808841561163',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth: true
});
and i have a separate js file with a function:
function example(){
FB.api(
'/me/[namespace]:visit',
'post',
{ channel: link},
function(response) {
if (!response || response.error) {
console.log(response.error);
} else {
console.log('Follow was success! Action ID: ' + response);
}
});
}
when i call this i get FB is undefined.
When i put the function inside window.fbAsyncInit it works ok , but i need to call the FB outside window.fbAsyncInit.
If there any possible way to do that?
just queue your function, and then call it right after FB initialized. Following code guarantees that your functions will be called in right order, and right after FB finish initialization
helper script you include BEFORE your example and before FB init script:
var FB; // to avoid error "undeclared variable", until FB got initialized
var myQueue = new Array();
function queueAdd(f){
if (FB == undefined)
myQueue.push(f);
else
f();
}
function processQueue(){
var f;
while(f = myQueue.shift())
f();
}
your function example:
function example(){
FB.api(
'/me/[namespace]:visit',
'post',
{ channel: link},
function(response) {
if (!response || response.error) {
console.log(response.error);
} else {
console.log('Follow was success! Action ID: ' + response);
}
});
}
queueAdd(example); // will run immediately if FB initialized. Otherwise, wait for FB first
and FB part:
window.fbAsyncInit = function() {
FB.init(blablabla);
processQueue();
}

Facebook API feed dialog - prevent unnecessary click

hi,
Using Facebook's API it's possible to prompt a user to post to his/her wall using the feed dialog. However, using the javascript SDK, this requires two clicks: one on the button which brings up the dialog, the other on the "Share" button within the dialog.
Is it possible to get rid of one of these clicks? I thought of two approaches:
Embedding the dialog within an iframe, as Facebook provides a URL for a full page dialog. That will require the user to click only on the "Share" button. Apparently Facebook blocks that option.
Using an access token and display=iframe, but I want to avoid the user having to authorize my application.
Any ideas?
i would suggest you to use fb.api instead.
function PostToMyWall(){
FB.login(function(response)
{
if (response.authResponse)
{
//Post to my wall
FB.api('/me/feed', 'post', {
message: lnk,link: lnk
},
function(response) {
if (!response || response.error) {
// //alert('Error occured');
} else {
// //alert("Successfully Posted to My Wall!");
}
});}else
{
alert('Not logged in');
}}, { scope : 'publish_stream' });
}
My application got a similar function, check it out at www.wootube.woolei.com - "Post to WooTube Page and Group"
or popup when the page using jquery
$(document).ready(function() { FB.ui({method: 'feed',
name: mdtxt,
link: lnk,
picture: hackImageUrl(img,fld),
caption: '<?php echo $clickme; ?>',
description: '<?php echo $app_desc; ?>'
},
function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert("Successfully Posted to Group");
}
});});