Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post to Facebook wall/feed using AJAX/jQuery

pulling my hair out over this one.

I'm writing a Facebook-connected web app that will ultimately post some arbitrary information to the walls of friends that are selected throughout the process.

I'm in the final stages now and was hoping that posting to the walls would be simple, but I've spent far too long trying to figure this out now so I'm hoping someone can help me.

I'm trying to post using ajax like so:

$.ajax({
    type: 'POST',
    url: "https://graph.facebook.com/bbeckford/feed",
    data: {message: wallMessage, target_id: friendID, access_token: "<?= $cookie['access_token'] ?>", format: "json"},
    success: function(data) { alert(data); },
    dataType: "JSON"
});

But I just keep getting this error: "XMLHttpRequest cannot load https://graph.facebook.com/bbeckford/feed. Origin http://www.secretsantasetup.com is not allowed by Access-Control-Allow-Origin."

I've done searches and one suggestion is to make a php proxy, is that a viable option? How would I go about doing that?

Am I approaching this completely wrong??

Any help would be greatly appreciated, Thanks, -Ben

EDIT I want to do this in the background, ie user has selected 10 friends for example, then on submit the app will loop through each friend and post something on their wall. Is this possible? Thanks!

EDIT 2 The test console at the bottom of the following page does exactly what I want to do, but there is no source code? - http://developers.facebook.com/docs/reference/rest/stream.publish

like image 570
bbeckford Avatar asked Jan 22 '23 02:01

bbeckford


1 Answers

Here you go using javaScript SDK

STEP 1

At first, your application should obtain the permission for posting on user's wall or user's friends wall.

sample code from Facebook : link

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
    } else {
      // user is logged in, but did not grant any permissions
    }
  } else {
    // user is not logged in
  }
}, {perms:'read_stream,publish_stream,offline_access'});

publish_stream is required to post on user's or user's friends wall.

Line which one need to edit : {perms:'read_stream,publish_stream,offline_access'})

To read more about other permissions : link

STEP 2

Taken from facebook JavaScript SDK Pages and tweaked link

   FB.ui(
   {
     method: 'stream.publish',
     message: 'getting educated about Facebook Connect',
     attachment: {
       name: 'Connect',
       caption: 'The Facebook Connect JavaScript SDK',
       description: (
         'A small JavaScript library that allows you to harness ' +
         'the power of Facebook, bringing the user\'s identity, ' +
         'social graph and distribution power to your site.'
       ),
       href: 'http://github.com/facebook/connect-js'
     },
     target_id: 'ENTER YOU FRIENDS IDS - more than one, seperate by commas',
     action_links: [
       { text: 'Code', href: 'http://github.com/facebook/connect-js' }
     ],
     user_message_prompt: 'Share your thoughts about Connect'
   },
   function(response) {
     if (response && response.post_id) {
       alert('Post was published.');
     } else {
       alert('Post was not published.');
     }
   }

 );

Line which one needs to edit or add : target_id: 'ENTER YOU FRIENDS IDS - more than one, seperate by commas',

:)

like image 133
sashtinathan Avatar answered Jan 31 '23 01:01

sashtinathan