mavame
mavame

Reputation: 399

Like facebook business page using built-in like open-graph api not working with javascript sdk

How do I like a Facebook business page once the user has logged in and authorized my app to Publish on his/her behalf? Here's my code so far:

window.fbAsyncInit = function() {
    FB.init({ appId: 'MY_APP_ID',                    
        status: true, 
        cookie: true,
        xfbml: true,
        oauth: true});

        document.getElementById('fb-auth').addEventListener('click', function() {
          FB.login(function(response){
            if(response.authResponse){ // user has authorized app
                FB.api('/me/likes/BUS_PAGE_ID', function(response){
                    if(response.data.length>0) {
                       // user has liked the page
                    } else {
                       // like the page for the user                                  
                    }
                });
            }
          }, {scope:'publish_actions, user_likes'});
        }, false);
};

The code is working to the point that it authorizes the app and I can see if the business page is liked by the user with FB.api(...) if(response.data.length>0). What do I put in the place of // like the page for the user so that my app can like a specific business page and add it to the user's likes? Thank you.

Upvotes: 1

Views: 2421

Answers (1)

phwd
phwd

Reputation: 19995

Actions can only be used for objects outside of Facebook.

You cannot use the built-in like action and you definitely cannot force a user to like a Facebook page.

Use the Like box or redirect the user to your Facebook page.

Upvotes: 6

Related Questions