Search code examples
facebookfacebook-graph-apifacebook-javascript-sdkfacebook-timeline

Publishing on users time line using facebook javascript sdk


I have been using Graph API along with javascript facebook sdk to make posts on users wall. The code looks like this

function graphStreamPublish(){
                showLoader(true);

                FB.api('/me/feed', 'post', 
                    { 
                        message     : "Sample Message",
                        link        : 'link url',
                        picture     : 'image url',
                        name        : 'app name',
                        description : 'Test stuff'

                }, 
                function(response) {
                    showLoader(false);

                    if (!response || response.error) {
                        alert('Error occured');
                    } else {
                        alert('Post ID: ' + response.id);
                    }
                });
            }

Can this piece of code be used to post on users timeline as well or is there something else I need to do?


Solution

  • The following code works. ( Notice the 'publish_actions' permission used at the end).

    FB.login(function(response) {
        // handle the response
    
            if (response.status === 'connected') {// Logged into your app and Facebook.
    
                            FB.api('/me/feed', 'post', 
                                { 
                                    message     : "Sample Message",
                                    name        : 'app name',
                                    description : 'Test stuff'
    
                                }, 
                                function(response) {
    
                                    if (!response || response.error) {
                                        alert(response.error);
                                    } else {
                                        alert('Post ID: ' + response.id);
                                    }
                                });
              }
    
    
    }, {scope: 'public_profile,email,publish_actions'} );