简体   繁体   中英

Javascript SDK not posting to facebook

        <script type="text/javascript">

        function SendToFacebook()
        {
            window.fbAsyncInit = function () {
                // init the FB JS SDK
                FB.init({
                    appId: '432036336937975',                        // App ID from the app dashboard
                    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) {
if (response.authResponse) {
        //simple user access token
        var accessToken = response.authResponse.accessToken,
            ajaxRequest = new XMLHttpRequest(),
            pageId = '1521533601413118';

        ajaxRequest.onreadystatechange = function() {
            if(ajaxRequest.readyState === 4) {
                //print out the extended page access token
                alert(ajaxRequest.responseText);
            }
        };
        ajaxRequest.open('POST','generatePageToken.php?pageId=' + pageId, true);
        ajaxRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        ajaxRequest.send('accessToken=' + accessToken);
    }

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

                        FB.api('/1521533601413118/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>

I get this error "[object Arguments]{0: Object {...}, length: 1}" in my javascript console. Why is this? Also I should note that I want this posted as the PAGE not as admin. Unsure of which it will do. I've been stuck at this for literally days so PLEASE can someone help?

I've read probably 100 threads from here and other places about this so I do realize this is a duplicate thread of sorts, but NO THREAD has yet to work for me. Want to link to me a thread? Probably already tried. I must be missing a setting or something. Please treat me like a complete beginning in this aspect - facebook API is so confusing.

Make sure you are familiar with the different Access Tokens of the Graph API. You need a Page Access Token to post "as Page". Apart from that it´s exactly the same API call. Here are some useful Links about the Tokens and how to get them:

The last Link explains how you create an Extended Page Tokens that is valid forever. You have to do this server side, but you don´t need the PHP SDK for it. It is a good idea to use the Page Token on the server only.

If you want to use the PHP SDK for the feed posting, here´sa tutorial:

About your code: You should initialize Facebook on page load and only call FB.login in your SendToFacebook function. Also check out "FB.getLoginStatus" in the Facebook docs, so you don´t need to use FB.login all the time.

Example call after getting the Extended Page Token (just an example, you may need to urlencode parameters like the message):

$appsecretProof = hash_hmac('sha256', $accessToken, $fbAppSecret);
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook-php-3.2');

//get extended user access token
$url = 'https://graph.facebook.com/' . $pageId . '/feed?message=whatever' .
    '&access_token=' . $pageToken;
    '&appsecret_proof=' . $appsecretProof;
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);

Check out the other parameters in the docs: https://developers.facebook.com/docs/graph-api/reference/v2.1/page/feed

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