简体   繁体   English

新的Facebook API问题

[英]New Facebook API issue

Can anyone help me with this code and tell me what is wrong? 谁能帮我这个代码,告诉我哪里出了问题?

My code is not working anymore with the new Facebook API and I am getting the below errors! 我的代码无法再使用新的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

And even if I changed it, it still does not work at all! 即使我更改了它,它仍然根本不起作用!

//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
}

Did you make the changes needed to support Oauth 2? 您是否进行了支持Oauth 2所需的更改? it's been mandatory since Oct 1st but the SDKs were only forced onto Oauth 2 yesterday (December 13 2012) 自10月1日起强制执行此操作,但SDK仅在昨天(2012年12月13日)被强制使用Oauth 2

See https://developers.facebook.com/docs/oauth2-https-migration/ - there's a summary of what changed and links to the announcing blog posts - the authentication and javascript docs are the docs you're most likely to need to check if you're making changes as this is where the changes were 请参阅https://developers.facebook.com/docs/oauth2-https-migration/-有关更改内容的摘要以及指向即将发布的博客文章的链接-身份验证和javascript文档是您最可能需要的文档检查您是否要进行更改,因为这是更改的地方

We were also affected by this change and we made the following changes to make it work. 我们也受到此更改的影响,我们进行了以下更改以使其起作用。 FB.init() was required as coded below with oauth set as true. FB.init()是按以下代码要求的,并且oauth设置为true。

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

And now there is no more session, response.session becomes response.authResponse and response.session.uid becomes response.authResponse.userID 现在不再有会话, response.session变为response.authResponseresponse.session.uid变为response.authResponse.userID

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

Link with additional information on js changes required https://developers.facebook.com/blog/post/525/ 链接有关js更改的其他信息,要求https://developers.facebook.com/blog/post/525/

Also make sure you're using FB.getAuthResponse() instead of FB.getSession(). 还要确保您使用的是FB.getAuthResponse()而不是FB.getSession()。

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

This is the Oauth 1.0 code: 这是Oauth 1.0代码:

html html

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

javascript 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
        }
    });
}

Changes I made to adapt to Oauth 2.0: 我为适应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
        }
    });
}

Additional: Here is the code that handles the response (ASP.NET MVC, C#): 附加:这是处理响应的代码(ASP.NET MVC,C#):

using Newtonsoft.Json.Linq; 使用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

    }

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

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