Jimmy
Jimmy

Reputation: 285

Post feed with js sdk to pagewall

Im trying to send posts to a wall of a page im an admin to through the js sdk. I have made it do i can do it for myself(my personal user) but i cannot get it to work as the "page".

<div id="fb-root"></div>
    <script type="text/javascript">
        function SendToFacebook()
        {
        window.fbAsyncInit = function () {
            // init the FB JS SDK
            FB.init({
                appId: '**CORRECT ID**',                        // App ID from the app dashboard
                channelUrl: '**MYPAGE**/channel.html', // Channel file for x-domain comms
                status: false,                                 // Check Facebook Login status
                xfbml: true                                  // Look for social plugins on the page
            });

            FB.ui(
               {
                   method: 'feed',
                   name: 'MyFeed',
                   link: '',
                   // picture: '',
                   caption: 'My caption',
                   description: 'test',
                   message: 'MESSAGE?',
               },
               function (response) {
                   if (response && response.post_id) {
                       alert('Delat på facebook.');
                   } else {
                       alert('Inte delat på facebook.');
                   }
               }
             );
            // Additional initialization code such as adding Event Listeners goes here
            };

        // Load the SDK asynchronously
        (function (d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) { return; }
            js = d.createElement(s); js.id = id;
            js.src = "//connect.facebook.net/en_US/all.js";
            fjs.parentNode.insertBefore(js, fjs);
        } (document, 'script', 'facebook-jssdk'));
        }
    </script>

This posts to my own user page, and ive tried the "to" parameter but it send the post from my personal user to the page. Is there any way to make this though the login function? bare in mind that im fairly new to this so demos and examples are welcome.

Upvotes: 2

Views: 1800

Answers (2)

Lasto
Lasto

Reputation: 127

I found this solution and I tried it, although I dont know how it was 3 years ago, now you need to append page access token you receive via /me/accounts calling (also you must be admin of that page etc.) so todays working solution could look like this:

FB.api('/me/accounts', function (apiresponse) {
              console.log(apiresponse);
              var data = {
                  message: "mymessage",
                  //display: 'iframe',
                  caption: "caption",
                  picture: 'www.bla.com/image.jpg',
                  link: 'www.facebook.com',
                  name: "name",
                  description: "description",
                  to: **APP IP**,
                  from: **APP IP**,
                  access_token: apiresponse.data[0].access_token
              };

              FB.api('/**APP IP**/feed', 'post', data, function () {
                  console.log(arguments);
              });
          });

Upvotes: 0

Jimmy
Jimmy

Reputation: 285

I got it to work after a hard days work, if any1 is interested heres how i did.

<script type="text/javascript">

        function SendToFacebook()
        {
            window.fbAsyncInit = function () {
                // init the FB JS SDK
                FB.init({
                    appId: '***',                        // App ID from the app dashboard
                    channelUrl: '***', // Channel file for x-domain comms
                    status: false,                                 // Check Facebook Login status
                    xfbml: true                                  // Look for social plugins on the page
                });


                FB.login(function (response) {
                    FB.api('/me/accounts', function (apiresponse) {

                        var data = {
                            message: "mymessage",
                            display: 'iframe',
                            caption: "caption",
                            name: "name",
                            description: "description",
                            to: **wallid**,
                            from: **wallid**
                        };

                        FB.api('/**wallid**/feed', 'post', data, function () {
                            console.log(arguments);
                        });


                    });

                }, { scope: 'manage_pages' });

            };
            // Load the SDK asynchronously
            (function (d, s, id) {
                var js, fjs = d.getElementsByTagName(s)[0];
                if (d.getElementById(id)) { return; }
                js = d.createElement(s); js.id = id;
                js.src = "//connect.facebook.net/en_US/all.js";
                fjs.parentNode.insertBefore(js, fjs);
            } (document, 'script', 'facebook-jssdk'));
        }
        </script>

Upvotes: 5

Related Questions