How to post to Facebook fanpage as fanpage, not user

早过忘川 提交于 2020-01-01 06:08:08

问题


How can I post on fanpage wall as fanpage not as a user - using javascript sdk.

RIght now on Init im receiving menage_pages and acquiring suitable fanpage id, how can i change call below?

var target = '/'+params.target+'/feed'

FB.api(target,
        'post',
        { message: params.message,
          link: params.link,
          picture: params.picture,
          caption: params.caption,
          description: params.description,
          name: params.name
          }
        ,function(response) {
            if (!response || response.error) {
                $("#error").removeClass('hidden');
            } else {
                $("#success").removeClass('hidden');
            }
        });

回答1:


You need the following permissions:

  • publish_stream
  • manage_pages

Now we call the page object to retrieve the page's access_token and then post with that token, something like:

function postToPage() {
    var page_id = 'MY_PAGE_ID';
    FB.api('/' + page_id, {fields: 'access_token'}, function(resp) {
        if(resp.access_token) {
            FB.api('/' + page_id + '/feed',
                'post',
                { message: "I'm a Page!", access_token: resp.access_token }
                ,function(response) {
                    console.log(response);
            });
        }
    });
}

Result:

More about this in my tutorial.




回答2:


All you need is those three functionalities to be implemented:

First one is for setting the basic parameters for your Application(XXX-APP needs to be changed with your real id of application).

Second, function fol login to Facebook and granting needed permissions to be able to post to Facebook.

Third function is for posting to Facebook(XXX-page need to be changed with ID of the Page that you want to post something)

FB.init({ appId: 'XXX-APP', status: true, cookie: true, xfbml: true, oauth: true });
access_token = '';
function loginFB(){
  FB.login(function(response) {
   if (response.authResponse) {
     access_token =   FB.getAuthResponse()['accessToken'];
     console.log('Access Token = '+ access_token);
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 }, {scope: 'publish_stream,manage_pages'});
}

function postToPage() {
    FB.api('/XXX-page', {fields: 'access_token'}, function(resp) {
        if(resp.access_token) {
            FB.api('/' + page_id + '/feed',
                'post',
                { message: "MSG", access_token: resp.access_token }
                ,function(response) {
                    console.log(response);
            });
        }
    });
}


来源:https://stackoverflow.com/questions/8034753/how-to-post-to-facebook-fanpage-as-fanpage-not-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!