在使用Graph API获取信息时遇到问题

时间:2012-11-04 17:35:26

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

我正在尝试使用Facebook Graph API来获取有关通过Facebook登录的用户的信息。

一开始,我只使用了一个功能,没有发生错误;但后来,我添加了另一个使用Facebook API的功能。从那一刻起,这些功能都不起作用。只有当我删除新功能时,旧功能才会再次工作......

我想这些代码比我上面描述的更容易理解:

    function fb_login(){
        FB.login(function(response) {

            if (response.authResponse) {
                access_token = response.authResponse.accessToken; //get access token
                user_id = response.authResponse.userID; //get FB UID

                FB.api('/me', function(response) {
                    user_email = response.email; //get user email

                            $.ajax({
                              type: "POST",
                              url: "http://*******/ajax.php",
                              data: { action: "login", id: user_id, email: user_email, first_name: response.first_name, last_name: response.last_name }
                            }).done(function( msg ) {
                              alert( "Data Saved: " + msg );
                            });            
                });

        } else {
            //user hit cancel button
            console.log('User cancelled login or did not fully authorize.');

        }
    }, {
        scope: 'publish_actions,email,read_stream,publish_stream'
    });
}

除非存在以下功能,否则上述功能将起作用。

function update_autopost(form)
{
    FB.api('/me', function(response) {
                    var autopost_value = form.autopost.checked;

                    if(autopost_value == false)
                    {
                        autopost_value = "off";
                    }
                    else{
                        autopost_value = "on";
                    }

                    $.ajax({
                      type: "POST",
                      url: "http://*********/ajax.php",
                      data: { action: "update", id: response.authResponse.userID, autopost: autopost_value }
                    }).done(function( msg ) {
                      alert( "Data Saved: " + msg );
                    });            
        });
}

更详细一点,在添加第二个函数后,会发生两件事:

  1. 第一个功能停止工作。
  2. 第二个功能无法从Facebook Graph API获取信息(例如, response.authResponse.userID 不起作用)。

0 个答案:

没有答案