FB Automatic Wall Post - javascript

I've implemented Wall post on the loggedin user with Graph API, its working fine i want if user clicks any button on my website(for eg if he purchase any thing) this will published on his wall,below is my code but in this code it is opening Publish dialog and i want to publish automaticallly(offcourse only when he logged in with fb and already has authorized)
FB.ui(
{
method: 'feed',
name: 'Transaction succeeded',
link: 'http://abc.com/',
description: 'Success'
},
function(response) {
if (response && response.post_id) {
} else {
}
}
);

Use FB.api to post feed stories once user is authorized and granted publish_stream permission to your application:
FB.api('/me/feed', 'post', {
name: 'Transaction succeeded',
link: 'http://abc.com/',
description: 'Success'
}, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});

You can publish to the Facebook graph by issuing HTTP POST requests to the appropriate connection URLs, using an access token. For example, you can post a new wall post on XYZ's wall by issuing a POST request to https://graph.facebook.com/XYZ/feed:
curl -F 'access_token=...' -F 'message=Hello, XYZ. I like this new API.'
https://graph.facebook.com/XYZ/feed
you can use curl request in .net

Related

Simple Facebook Javascript post to stream example?

I'm trying to post to user stream, without user prompt. I cannot manage to find a code that works. Facebook JSDK is already loaded and I will insert the code inside:
FB.getLoginStatus(function(response){
to make sure the user is already logged to my application. Could you provide an example of publishing to the user stream using the publish_stream permission?
With Dialog
You need to use the Feed Dialog, with FB.ui():
function postToFeed() {
// calling the API ...
var obj = {
method: 'feed',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
name: 'Facebook Dialogs',
caption: 'Reference Documentation',
description: 'Using Dialogs to interact with users.'
};
function callback(response) {
document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
}
Documentation: https://developers.facebook.com/docs/reference/dialogs/feed/
Without Dialog
To make a post without the Dialog you need to use the FB.api():
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);
}
});
Documentation: https://developers.facebook.com/docs/reference/javascript/FB.api/
Direct URL
https://www.facebook.com/dialog/feed?
app_id=APP_ID&
link=https://YOUR_DOMAIN&
picture=http://YOUR_DOMAIN/image.jpg&
name=Facebook%20Dialogs&
caption=API%20Dialogs&
description=Using%20Dialogs%20to%20interact%20with%20users.&
redirect_uri=http://YOUR_DOMAIN/response
Documentation: https://developers.facebook.com/docs/reference/dialogs/feed/

Facebook JS - FB.api() only posts message, not media attachment

I'm trying to post to the active users wall with a mp3 attachment. It works fine from
Facebook's Test Console (see below), but when I call it from my mobile app, It only posts the message. What am I missing here :(
Facebook's Test Console: http://developers.facebook.com/docs/reference/rest/stream.publish/
Here is my JS...
Login
FB.login(
function(response) {
if (response.authResponse) {
alert('logged in');
} else {
alert('not logged in');
}
},{ scope: "email,user_likes,publish_stream,offline_access" } //added offline_access to see if that was the problem
);
Post to wall with attachment
var attachment = {
'message': 'testing',
'attachment': {'media': [{
'type': 'mp3',
'src': 'http://www.looptvandfilm.com/blog/Radiohead%20-%20In%20Rainbows/01%20-%20Radiohead%20-%2015%20Step.MP3',
'title': 'Test Title',
'artist': 'My Artist',
'album': 'My Album' }]}
};
FB.api('/me/feed', 'post', attachment, function(response) {
if (!response || response.error) {
alert(response.error.message);
} else {
alert('Post ID: ' + response.id);
}
});
var attachment = {
'message': 'testing',
'source': 'http://www.looptvandfilm.com/blog/Radiohead%20-%20In%20Rainbows/01%20-%20Radiohead%20-%2015%20Step.MP3'
};
This will post the mp3 to your feed allowing a user to click a play button inline. See working example here: http://jsfiddle.net/dmcs/aggJc/1/
As you will note, that when you POST data to the Graph API, the formatting is different than when you GET the same object back. Posting is kinda shorthand notation version and the Getting is the long hand.

Fb.api post to user wall only on login

I would like to use fb.api to post on logged user, but just once. If I put this
var params = {};
params['message'] = 'gegeegeggegall! Check out www.facebook.com/trashcandyrock for more info.';
params['name'] = 'gegeggeeg - gegegege';
params['description'] = 'Check out Tegegegeg! Win merch by playing and reccomending to your friends.';
params['link'] = 'http://www.bblblba.com';
params['picture'] = 'http://summer-mourning.zoocha.com/uploads/thumb.png';
params['caption'] = 'Tgegegegeeg';
FB.api('/me/feed', 'post', params, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Published to stream - you might want to delete it now!');
}
});
It posts to users wall everytime he refreshes the site?
What to do?
What is triggering the FB.api call? If it's just code within a tag then it's going to run as soon as the browser gets to that point.
You could possibly store some sort of cookie value or something after the FB.api call then check it on page load, but that seems like more work than is probably needed.
Do you want him to post it only once, ever?
If so, you're going to need to create a "state". In order to do this, you could do it client sided (with cookies), or server sided (with a database).
Create a boolean variable named "posted", and store it in a cookie or in a database (since you're using javascript, it's probably easier to use a cookie).
var posted=getCookie("posted");
if(!posted)
{
//call the FB.api();
setCookie("posted", true, duration);
}
Definition of setCookie and getCookie: http://www.w3schools.com/JS/js_cookies.asp
You could run a FQL query and check to see if the message has already been posted by querying the stream table with your app id. Something like:
<!DOCTYPE html>
<html>
<body>
<div id="fb-root"></div>
Post To Wall
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({ appId : '**yourAppID**', status : true, cookie : true, xfbml : true });
function postToWall() {
FB.login(function(response) {
if (response.session) {
FB.api(
{
method: 'fql.query',
query: 'SELECT post_id, message from stream where app_id = **yourAppID** and source_id = me()'
},
function(response) {
if(response.length == 0){
FB.ui(
{
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.',
message: 'Facebook Dialogs are easy!'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
}
else {
alert('User already posted this message');
}
}
);
}
} , {perms:''});
}
</script>
</body>
</html>

How to post to an Authorised User (with extended permissions) wall - Facebook JS SDK + Graph API

I know this question has been asked a thousand time, but I have yet to come across a definitive answer.
How does an application (external website, so no fbml) post to a users wall, using JS and Graph API?
I have established extended permissions with users, and can post to walls using Facebooks sample code, however the variables within the script wont appear on FB.
For Example:
function publish_test(){
var body = 'Reading Connect JS documentation';
FB.api('/me/feed', 'post', { body: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response);
}
});
}
This posts to the users wall, however it does not contain the body. Any help on this would be gratefully appreciated.
FB.ui will pop up a confirmation dialog to the user, not sure if you want that or not.
http://developers.facebook.com/docs/reference/javascript/FB.ui
This page has documentation about the FB.api call:
http://developers.facebook.com/docs/api
Scroll down to the "Publishing" section and notice that there's no "body" parameter specified. There is a "message" parameter though. The other example you're looking at is probably incorrect?
I got this code working
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);
}
});
}

Using Facebook Graph to simply post a wall message with just javascript

In Facebook how can I post a message onto a user's wall saying "I scored 8/10 on objects game" then a URL?
I really don't want to have to use the full API, as I don't want to handle user login details. I don't mind if Facebook needs to authenticate and then post the message.
Is it possible using the new Graph API and JavaScript?
Note 4/16/2011: stream.publish seems to have been deprecated, There's a new way to do this: http://developers.facebook.com/docs/reference/dialogs/feed/
You can use something like this to publish to a wall, the user will need to confirm before it get sent.
Don't forget that you'll need to use FB.init and include the JS SDK link.
function fb_publish() {
FB.ui(
{
method: 'stream.publish',
message: 'Message here.',
attachment: {
name: 'Name here',
caption: 'Caption here.',
description: (
'description here'
),
href: 'url here'
},
action_links: [
{ text: 'Code', href: 'action url here' }
],
user_prompt_message: 'Personal message here'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
}
Post on wall will show a dialog box to share the message on wall or not. But I wanted to post the message silently on user's wall, assuming that user had already given "Post on wall" permission.
FB.api('/me/feed', 'post', {
message:'my_message',
link:YOUR_SITE_URL,
picture:picture_url
name: 'Post name',
description: 'description'
},function(data) {
console.log(data);
});
Considering that you have a proxy to make cross domain calls, you can simply do this...
In this example, YourProxyMethod takes a jQuery.ajax like hash, makes a server side post & returns the response in success/error callbacks. Any regular proxy should do.
The trick is to include app_id and access_token in the URL irself.
Also, your FB app should have sufficient permissions to make this call.
YourProxyMethod({
url : "https://graph.facebook.com/ID/feed?app_id=APP_ID&access_token=ACCESS_TOKEN",
method : "post",
params : {
message : "message",
name : "name",
caption : "caption",
description : "desc"
},
success : function(response) {
console.log(response);
},
error : function(response) {
console.log("Error!");
console.log(response);
}
});

Categories

Resources