2

In my Facebook application, I am requesting 3 scopes: email,publish_stream,publish_action

I am using the FB.login function. These 2 steps pop up.

When the user clicks "Cancel" in the first step, FB.login will show "status: unknown" as the response object.

However, when user clicks cancel in the second step, FB.login shows it as "status:connected" and treats it as if the user accepted everything.

I recently learned that you can check if the user allowed the 2nd step using

FB.api('/me/permissions', function (response) {
    console.log(response);
} );

My question is...knowing that the user denied the open graph step, how can I pop that dialog up again?

enter image description here enter image description here

4

1 回答 1

1

You are correct, the 2nd stage of the Auth Dialog is optional, the user does not have to accept all of the extended permissions you ask for, or any of them, as it states in the Permissions sections of the auth dialog documentation:

The user will be able to remove any of these permissions, or skip this stage entirely, which results in rejecting every extended permission you've requested. Your app should be able to handle revocation of any subset of extended permissions for installed users.

The best approach I think is to have your app manage with what the user accepts, but if you HAVE to have the permission(s) in the optional stage (extended permissions) then this is what you can do:

FB.init({
    ....
});

var requiredPermissions = ["email", "publish_stream", "publish_action"];

function checkPermissions(response) {
    var ok = true;

    if (!response.data || response.data.length != 1)
        ok = false;
    else for (var perm in requiredPermissions) {
        if (!(perm in response.data[0])) {
            ok = false;
            break;
        }
    }

    if (!ok)
        login();
    else
        console.log("Hey there user who granted all the required permissions");
}

function loginCallback(response) {
    if (response.authResponse) {
        FB.api("/me/permissions", checkPermissions);
    }
    else {
        console.log("User cancelled login or did not fully authorize.");
    }
}

functoin login() {
    FB.login(loginCallback, { scope: requiredPermissions.join(",") });
}

I haven't tested this code, it's a nudge in the right direction though. Also, this code will go on forever until the user accepts all permissions or just gives up, you should somehow let him know that you need those permissions and about to send him for the auth dialog again.


Edit

I keep forgetting to include this with my answers: Calling FB.login opens a new pop-up window, and browsers usually blocks that unless it's a result of a user action, as it says in the docs:

Calling FB.login results in the JS SDK attempting to open a popup window. As such, this method should only be called after a user click event, otherwise the popup window will be blocked by most browsers.

It also says there:

If you need to collect more permissions from users who have already authenticated with your application, call FB.login again with the permissions you want the user to grant. In the authentication dialog, the user will only ever be asked for permissions they have not already granted.

Which means that it's a way to do what you want, the popup probably did not open because it was blocked by your browser. You need to modify the code I gave you so that every time you call the login function it's after the user interacted with your page, i.e.: display a message saying "the application needs this and that permissions, please click this button to continue" which then will call the login function.

于 2012-04-06T08:49:42.633 回答