简体   繁体   中英

Facebook - How can i post to a company using the javascript sdk?

Im new to facebook posting but have had some success with posting offline with a user account but cannot post offline with a company page.

I have created my own "Facebook App" called "Nicks Poster App" via my own personal facebook account. I have granted three permissions (offline_access,read_stream,publish_stream) to the app for both my personal page and my company page. i did this by following these steps for each account...

Creating the app...
1.       Login to facebook with the account you want linked to the app
2.       Follow this link http://www.facebook.com/developers/apps.php#!/developers/createapp.php
3.       Create your app and take a note of you App Id and your App secret Id.

Giving the correct rights to the app and getting the access_token..
Method 1:
1.       Get the account in question to login to facebook
2.       However you like, direct the user to this link (replacing <App-Id> with the App Id of the created app) https://graph.facebook.com/oauth/authorize?client_id=<App-Id>&scope=offline_access,read_stream&redirect_uri=http://www.facebook.com/connect/login_success.html
3.       Take a note of the result of the “code” querystring.
4.       Goto this url (replace “<APP-ID>” with you appId and “<APP-SECRET>” with your apps secret id and “<code>” with the copied code)
https://graph.facebook.com/oauth/access_token?client_id=<APP-ID>&redirect_uri=http://www.facebook.com/connect/login_success.html&client_secret=<APP-SECRET>&code=<code>
5.       Copy what you see, minus the expires querystring.  That is your access_token.

After i had the access token for both accounts i used this code to make the post.

<!-- FACEBOOK -->        
<div id="fb-root"></div>
<script>
    (function () {
        var e = document.createElement('script');
        // replacing with an older version until FB fixes the cancel-login bug
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        //e.src = 'scripts/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());
</script>
<!-- END-OF-FACEBOOK -->
<script>
    //initialise
    window.fbAsyncInit = function () {
        FB.init({
            appId: '351023398277068',
            status: true, // check login status
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true, // parse XFBML
            oauth: true // Enable oauth authentication
        });
    };
    function sendPost(inMessage) {
        var opts = {
            message: inMessage,
            access_token: '<SAVED-TOKEN>'
        };

        FB.api('/me/feed', 'post', opts, function (response) {
            if (!response || response.error) {
                alert('Posting error occured');
            }
            else {
                alert('Success - Post ID: ' + response.id);
            }
        });
    }

</script>

When executing the "sendPost" command with the perameter 'Test post', it will work for my personal account (providing i put my access_token in place). This does not work for my company page, and im at a loss as to why(i do put my acess_token in place).

Facebok also havent documented this very well and it makes it hard to make progress, does anyone understand why this doesnt work for company pages?

Thank you in advance.

You can set the "to" parameter to target the page you wish to post to, "manage pages perms will be needed if you wish to post as your page to your page as the application.

<div id="msg"></div>
<script>
// uid is the id of the page or user you wish to post to.
      function feedthis2(uid) {

        // calling the API ...
        var obj = {
        method: 'feed',
        to: ''+uid+''    
        };

        function callback(response) {
          document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
        }

        FB.ui(obj, callback);
      }
    feedthis2('AnotherFeed'); // to http://facebook.com/anotherfeed
    //feedthis2('135669679827333');
</script>

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