新的Facebook API问题

时间:2011-12-14 13:52:59

标签: facebook api

任何人都可以帮我解决这个问题并告诉我出了什么问题吗?

我的代码不再适用于新的Facebook API,我收到以下错误!

Uncaught Error: OAuth2 specification states that 'perms' should now be called 
'scope'. Please update.

Error is in http://connect.facebook.net/en_US/all.js line 23

即使我改变它,它仍然根本不起作用!

//THIS FUNCTION WILL INITIALIZE THE FACEBOOK API AND WILL ADD NEW FACEBOOK ACCOUNTS.
var apikey = $("#apikey").val();
//var tuittingID = $("#tuittingID").val();
FB.init({ apiKey: apikey, status : true, cookie : true, oauth: true });
FB.getLoginStatus(handleSessionResponse);
$('#FBlogin').live('click', function() {
    FB.login(handleSessionResponse, {
        scope:'manage_pages, publish_stream, offline_access, user_status, 
        read_insights'
    });
    return false;
});
function handleSessionResponse(response) {
    if (!response.session || String(response.scope) == '') {
        return;
    }
    else
    var tuittingID = $.cookie("tuittingID");
    $('#AccessToken').val(response.session.access_token);
    $("#loader").show();
    $.post("tuitting/facebook_lib/fbadd.php",
    { tuittingID: tuittingID, uid: FB.getSession().uid, usid: 
    FB.getSession().session_key, accesstoken: response.session.access_token },
    function(data){
        reloadAccounts();
        $("#loader").hide();
        FB.logout(function(response) {
        }); //END LOGOUT FROM FACEBOOK AFTER SUCCESSFULL ACTION
    }
    ); //END AJAX POST FUNCTION DATA
}

4 个答案:

答案 0 :(得分:6)

您是否进行了支持Oauth 2所需的更改?它自10月1日起被强制执行,但SDK昨天(2012年12月13日)仅被强制进入Oauth 2

请参阅https://developers.facebook.com/docs/oauth2-https-migration/ - 其中包含已发布更改内容的摘要以及指向发布博客帖子的链接 - 身份验证和javascript文档是您最有可能需要检查是否进行更改的文档,因为这是变化的地方

答案 1 :(得分:1)

我们也受到此更改的影响,我们进行了以下更改以使其正常工作。 FB.init()需要编码如下,oauth设置为true。

        FB.init({
        appId: 1234567890123,
        oauth: true
    })

现在没有更多会话, response.session 变为 response.authResponse response.session.uid 变为响应.authResponse.userID

            FB.login(function(response) {
        if (response.authResponse) {
                var postData = {
                    FBID: response.authResponse.userID,
                    Access_Token: response.authResponse.accessToken
                };

链接有关所需js更改的其他信息 https://developers.facebook.com/blog/post/525/

答案 2 :(得分:0)

还要确保使用FB.getAuthResponse()而不是FB.getSession()。

http://developers.facebook.com/docs/reference/javascript/FB.getAuthResponse/ http://developers.facebook.com/blog/post/525/

答案 3 :(得分:0)

这是Oauth 1.0代码:

<强> HTML

<div id="fb-root"></div>
<div class="fb-login-button" scope="email,user_birthday" 
onlogin="afterFacebookConnect();" autologoutlink="false">Login con Facebook</div>

<强>的javascript

        window.fbAsyncInit = function () {
            FB.init({
                appId: 'id_of_app',
                status: true,
                cookie: true,
                xfbml: true                });
        };


        if (document.getElementById('fb-root') != undefined) {
            var e = document.createElement('script');
            e.type = 'text/javascript';
            e.src = document.location.protocol + '//connect.facebook.net/es_ES/all.js';
            e.async = true;
            document.getElementById('fb-root').appendChild(e);
        }


function afterFacebookConnect() {
    FB.getLoginStatus(function (response) {
        if (response.session) {
            window.location = "/facebook/login?token=" + response.session.accessToken;
        } else {
            // user clicked Cancel
        }
    });
}

我为适应Oauth 2.0所做的更改:

   window.fbAsyncInit = function () {
        FB.init({
            appId: 'id_of_app',
            status: true,
            cookie: true,
            xfbml: true,
            oauth:true // additional parameter
        });
    };

function afterFacebookConnect() {
    FB.getLoginStatus(function (response) {
        if (response.authResponse) {
            window.location = "/facebook/login?token=" 
           + response.authResponse.accessToken;
        } else {
            // user clicked Cancel
        }
    });
}

附加:以下是处理响应的代码(ASP.NET MVC,C#):

使用Newtonsoft.Json.Linq;

   [RequireHttps]
    public ActionResult login(string token)
    {
        WebClient client = new WebClient();

            string JsonResult = client.DownloadString(string.Concat(
                   "https://graph.facebook.com/me?access_token=", token));
            JObject jsonUserInfo = JObject.Parse(JsonResult);

            string nombre = jsonUserInfo.Value<string>("first_name");
            string segundo_apellido = jsonUserInfo.Value<string>("last_name");
            string nombre_completo = jsonUserInfo.Value<string>("name");
            string genero = jsonUserInfo.Value<string>("gender");
            string email = jsonUserInfo.Value<string>("email");
            string birthday = jsonUserInfo.Value<string>("birthday");

    // here you do your logic to login the user 
    // otherwise you can send user to the registration 
    // form and fill in some data you received from facebook

    }