简体   繁体   中英

How to request permissions for facebook app with JavaScript

How can I check the current user login status and ask for permissions for my app? This recently changed and now my app is not working anymore. Thanks for your help!

I always use this setup to handle login status and permissions:

function getPerms() {
    FB.login(function(response) {
        if (response.authResponse) {
            //user has granted permissions
        }
    }, {scope:"email, ..."});
}

function getStatus() {
    FB.getLoginStatus(function(response) {
        if (response.status === "connected") {
            //user has granted permissions
        } else {
            getPerms();
        }
    });
}

You still have to include the all.js and call FB.init(...)

Here is how you can do that:

    FB.init(
    {
        "appId"      : xxxxxxxxxx,
        "status"     : true,
        "cookie"     : true,
        "xfbml"      : true,
        "oauth"      : true
    });

    FB.Event.subscribe('auth.authResponseChange', function(response)
    {
        // need to wait few seconds for fb login status to work in FireFox
        setTimeout('window.location.reload()', 3000);
    });


function fbConnect(){
    FB.getLoginStatus(function(response) {
       if (response.authResponse){
        FB.login(function(response)
        {
            if (response.authResponse) { }
            else
            {
                //console.log('User cancelled login or did not fully authorize.');
            }

        }, {"scope":"email,manage_pages"});
       }
    });
}

You need to call fbConnect() function.

Also make sure that you have these below <body> tag on your page:

<div id="fb-root"></div>
<script src="//connect.facebook.net/en_US/all.js"></script>
  1. You need to provide some code to get better/accurate answers
  2. Most likely you didn't upgrade your code to use OAuth 2.0, see here
  3. Based on the link above, use scope instead of perms
  4. Use response.authResponse instead of response.session
  5. Welcome to Stackoverflow!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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