简体   繁体   English

Javascript SDK未发布到Facebook

[英]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. 我在JavaScript控制台中收到此错误“ [[Object Arguments] {0:Object {...},length:1}”。 Why is this? 为什么是这样? Also I should note that I want this posted as the PAGE not as admin. 另外,我应该注意,我希望将此内容发布为PAGE,而不是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. 我已经从这里和其他地方阅读了大约100个线程,因此我确实意识到这是重复的线程,但是没有螺纹适合我。 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. 请在这方面将我视为一个完整的开始-facebook API是如此令人困惑。

Make sure you are familiar with the different Access Tokens of the Graph API. 确保您熟悉Graph API的不同访问令牌。 You need a Page Access Token to post "as Page". 您需要页面访问令牌来发布“作为页面”。 Apart from that it´s exactly the same API call. 除此之外,它是完全相同的API调用。 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. 您必须在服务器端进行操作,但是不需要PHP SDK。 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: 如果您想使用PHP SDK进行提要发布,请参见以下教程:

About your code: You should initialize Facebook on page load and only call FB.login in your SendToFacebook function. 关于您的代码:您应该在页面加载时初始化Facebook,并且只能在SendToFacebook函数中调用FB.login。 Also check out "FB.getLoginStatus" in the Facebook docs, so you don´t need to use FB.login all the time. 还要在Facebook文档中签出“ FB.getLoginStatus”,因此您不必一直使用FB.login。

Example call after getting the Extended Page Token (just an example, you may need to urlencode parameters like the message): 获取扩展页面令牌后的示例调用(仅作为示例,您可能需要对诸如消息之类的参数进行urlencode):

$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 在文档中查看其他参数: https : //developers.facebook.com/docs/graph-api/reference/v2.1/page/feed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM