Post a public message in Facebook with FB.api - facebook

I have tried to post a message on my wall using FB.api(). I posted it successfully but it's showing only for me. I want to make this post public.
In app Configuring permissions i set "Default Activity Privacy" value Public but still message posting private.
I tried to do it by following code:
FB.login(function (response) {
if (response.authResponse) {
var privacy = { 'value': 'EVERYONE' };
var txt = 'my post to test feed post using api';
FB.api('me/feed', 'post', { message: txt, privacy: privacy }, function (response) {
if (!response || response.error) {
alert(JSON.stringify(response.error));
} else {
alert('Post ID: ' + response.id);
}
});
}
}, { scope: 'email,user_likes,publish_actions,publish_stream,read_stream' });
Please check my code.

As said in reference 'privacy' field value should be string.
Try to pass "{ 'value': 'EVERYONE' }" as string, not an object.

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) {

Facebook Graph API - Error on success?

I am using the Facebook Javascript SDK to upload a photo to the user's timeline. This is my call:
function post_photo_to_facebook() {
var file_name = 'https://my-image.url.com/image.jpg';
var access_token = FB.getAuthResponse()['accessToken'];
$.ajax({
type: "POST",
url: "https://graph.facebook.com/me/photos",
data: {
message: "Here is my message",
url: file_name,
access_token: access_token,
format: "json"
},
success: function(data){
alert("POST SUCCESSFUL");
},
error: function(data){
alert('Error');
console.log(data);
}
});
}
When in Chrome, I am receiving an Error back from this AJAX call, yet the statusText is "OK", and the image is being successfully uploaded to my timeline. I am just wondering what I am missing here - why is the error being called?
You should be using FB.api to upload images, rather than ajax POST due to CORS reasons.
So your code above would look like this:
var file_name = 'https://my-image.url.com/image.jpg';
FB.api('/me/photos', 'post', {
message:'Here is my message',
url:file_name
}, function(response){
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
and you use FB.init to set up your tokens etc.

Sencha Touch and Facebook button

I need to create a button to submit a comment on Face Book wall.
I am using MVC in Sencha touch and in my controller I use a function as
facebookComment: function()
{
--------
}
This function is to be called when a button is pressed.
Can any body please throw some light on how to go about this?
i am using following code to post on friends wall see if it is useful to you or not
var postParams = {
method: 'stream.publish'
, target_id: friend_id
, display: 'popup'
, message: message
, user_message_prompt: "Post message!"
}
FB.api(postParams, function(response) {
if(response.error_code || !response) {
to handle error
}
});
or refer this link https://developers.facebook.com/docs/reference/rest/stream.publish/
Well, first you should already be using the Javascript SDK. Then just issue a HTTP POST request to the feed connection of the current user using the FB.api() method with the message field set to your comment:
var body = comment_var;
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
Obviously, should be taking care of the user login status (maybe using the method FB.getLoginStatus()).

Check if Facebook JS-API app already has been given permission

I have a small script that unlocks some content on a page when a user logs in via Facebook and posts some information on their wall.
I want to avoid any duplicate posts (spam) in the event the user visits the page a second time and repeats the process.
Is there a way of (when asking for the permissions) to tell if they have already been given and therefore skip the sharing part of the script?
Here's my code:
$('.preview a').click(function(e) {
e.preventDefault();
FB.login(function(response) {
if (response.session) {
FB.api('/me/feed', 'post', {
message: 'Just unlocked the exclusive preview of...',
picture: 'http://website.com/share.png',
link: 'http://www.website.com',
name: 'Name goes here',
description: 'Description here'
},
function(response) {
if (!response || response.error) {
alert(response.error);
} else {
$('.preview').hide();
$('.clip').show();
}
});
}
},
{perms:'read_stream,publish_stream'});
});
From 2011 to 2014 a lot of things change..
I made this solution to check for "user_friends" and "publish_actions" permissions, if not allowed the both, force the user to "re-autenticate". The callback method will only be called when all permissions are given.
function login(cb,forceAuth) {
FB.getLoginStatus(function (response) {
if (response.status !== 'connected' || forceAuth==true){
FB.login(function (response) {
checkPermissions(cb,false);
}, {scope: 'publish_actions,user_friends'});
} else {
checkPermissions(cb);
}
});
}
function checkPermissions(cb,forceAuth){
FB.api(
"/me/permissions",
function (response) {
if (response.data[0]['publish_actions']==undefined || response.data[0]['publish_actions']==0 ||
response.data[0]['user_friends']==undefined || response.data[0]['user_friends']==0) {
if (forceAuth!=false)
login(cb,true);
} else {
cb();
}
}
);
}
How to use:
login(function () {
myLogedAndAllowedFunction();
});
I've written a tutorial about this here, and here's an example:
FB.api({ method: 'fql.query', query: 'SELECT read_stream,offline_access,publish_stream FROM permissions WHERE uid=me()' }, function(resp) {
for(var key in resp[0]) {
if(resp[0][key] === "1")
console.log(key+' is granted')
else
console.log(key+' is not granted')
}
});
There's also a JS REST example there in the article just for reference.

Is there a way to post a message to a Facebook group's feed?

I'm working on a Facebook app that uses Facebook groups, and I'd like to open up a Feed dialog to let the user post a notice to the group's feed. I have a user who is in the group and I've got the relevant permissions as well.
I've tried passing the group's Facebook id in the https://developers.facebook.com/docs/reference/dialogs/feed/ feed dialog, but get a Facebook error. There doesn't appear to be a programmatic way to do it either: the group's feed (described at https://developers.facebook.com/docs/reference/api/group/) isn't documented to accept POSTs either. Is there a way to post a message to the group using the Graph API or one of the other API methods?
I just made a test and it's working as any other wall.
I made a POST to 'GROUP_ID/feed' with a message variable with a value and it worked. I logged into the app as the group owner.
Using facebook php-sdk:
$facebook->api('GROUP_ID/feed', 'POST', array(
'message' => 'Testing...'
)));
I would suggest to use javascript instead as it wont cause post back of our website.
Check out my application to see if that what you want -> WooTube
function Promote() {
var lnk =
'http://www.wootube.woolei.com?v=<?php echo $_GET["id"] ?>';
FB.login(function(response) {
if (response.authResponse) {
//Post To WooTube Group
FB.api('/271691796276524/feed', 'post', {
message: lnk,
link: lnk,
},
function(response) {
if (!response || response.error) {
//alert('You have to join the group first!');
} else {
//alert("Successfully Posted to WooTube Group!");
}
});
//Post to Wootube Page
FB.api('/173724382762792/feed', 'post', {
message: lnk,
link: lnk
},
function(response) {
if (!response || response.error) {
//alert('You have to like http://www.facebook.com/WooTubes first!');
} else {
//alert("Successfully Posted to WooTube Page!");
}
});
} else {
alert('Not logged in');
}
}, {
scope: 'publish_stream'
});
}