Facebook JS SDK -Access Tokens无法发布操作

时间:2012-07-13 21:03:34

标签: facebook facebook-graph-api facebook-javascript-sdk

当我使用下面的代码没有任何反应时,我无法理解为什么会这样

function graphStreamPublish(){
var passString = '?[OBJECT]=http://entrancesuccess.net&access_token=' + accesstoken;

 FB.api('/me/[APPNAMESPACE]:[ACTION]' + passString,'post', 
            function(response) {
                showLoader(false);

                if (!response || response.error) {
                    alert('Error occured');
                } else {
                    alert('Post ID: ' + response.id);
                }
            });
        }

但是,当我只是简单地将访问令牌(YTWYDGFXVVZG456AVGAFV45HGCZXHGFACGFCGFXDG546FXGFXJGFXGFXGXFJXGFXG)复制粘贴到变量“passString”中时,一切正常,并且会在Facebook上的用户活动上发布操作。

示例:

  function graphStreamPublish(){
var passString = '?[OBJECT]=http://entrancesuccess.net&access_token=YTWYDGFXVVZG456AVGAFV45HGCZXHGFACGFCGFXDG546FXGFXJGFXGFXGXFJXGFXG';

其余代码如下(如果需要):

window.fbAsyncInit = function() {
            FB.init({ appId: 'XXXXXXXXXXXXX', //change the appId to your appId
                status: true, 
                cookie: true,
                xfbml: true,
                oauth: true});

           showLoader(true);

           function updateButton(response) {
                button       =   document.getElementById('fb-auth');
                userInfo     =   document.getElementById('user-info');

                if (response.authResponse) {
                    //user is already logged in and connected
                    FB.api('/me', function(info) {
                        login(response, info);
                    });

                    button.onclick = function() {
                        FB.logout(function(response) {
                            logout(response);
                        });
                    };
                } else {
                    //user is not connected to your app or logged out
                    button.innerHTML = 'Login';
                    button.onclick = function() {
                        showLoader(true);
                        FB.login(function(response) {
                            if (response.authResponse) {
                                FB.api('/me', function(info) {
                                    login(response, info);
                                });    
                            } else {
                                //user cancelled login or did not grant authorization
                                showLoader(false);
                            }
                        }, {scope:'publish_stream,public_action'});     
                    }
                }
            }

            // run once with current status and whenever the status changes
            FB.getLoginStatus(updateButton);
            FB.Event.subscribe('auth.statusChange', updateButton);  
        };
        (function() {
            var e = document.createElement('script'); e.async = true;
            e.src = document.location.protocol 
                + '//connect.facebook.net/en_US/all.js';
            document.getElementById('fb-root').appendChild(e);
        }());


        function login(response, info){
            if (response.authResponse) {
                 var accessToken                                 =            response.authResponse.accessToken;

                userInfo.innerHTML                             = '<img src="https://graph.facebook.com/' + info.id + '/picture">' + info.name
                                                                 + "<br /> Your Access Token: " + accessToken;
                button.innerHTML                               = 'Logout';
                showLoader(false);
                document.getElementById('other').style.display = "block";
            }
        }

        function logout(response){
            userInfo.innerHTML                             =   "";
            document.getElementById('debug').innerHTML     =   "";
            document.getElementById('other').style.display =   "none";
            showLoader(false);
        }

1 个答案:

答案 0 :(得分:0)

我会说您的函数accesstoken中没有变量graphStreamPublish

您在函数var中使用login关键字,这使其成为本地变量 - 因此,一旦该函数完成,它的内容就会被丢弃。< / p>

您可以在没有var的情况下执行作业,然后它就是全局变量,graphStreamPublish应该能够访问它。

但通常你根本不需要显式传递访问令牌,因为JS SDK完全能够在后台自行传递它,而不需要你自己主动传递它。