DiegoP.
DiegoP.

Reputation: 45737

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!

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
}

Upvotes: 1

Views: 6714

Answers (4)

Igy
Igy

Reputation: 43816

Did you make the changes needed to support Oauth 2? it's been mandatory since Oct 1st but the SDKs were only forced onto Oauth 2 yesterday (December 13 2012)

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

Upvotes: 6

Artur Kedzior
Artur Kedzior

Reputation: 4263

This is the Oauth 1.0 code:

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

Changes I made to adapt to 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#):

using 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

    }

Upvotes: 0

user_v
user_v

Reputation: 10114

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({
        appId: 1234567890123,
        oauth: true
    })

And now there is no more session, response.session becomes response.authResponse and response.session.uid becomes 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/

Upvotes: 1

qtrandev
qtrandev

Reputation: 16

Also make sure you're using FB.getAuthResponse() instead of FB.getSession().

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

Upvotes: 0

Related Questions