简体   繁体   中英

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);
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM