如何请求用户使用FB API授予对其信息的访问权限

时间:2019-02-06 03:06:28

标签: javascript facebook-graph-api facebook-javascript-sdk

我有下面的代码,该代码允许用户使用Facebook SDK API来注册我们的应用程序。它运行良好,但是我想请用户向我们授予其picture,birthday,address,email,gender,location和其他信息的权限。

window.fbAsyncInit = () => {
    //SDK loaded, initialize it
    FB.init({
        appId      : 'my-app-id',
        xfbml      : true,
        version    : 'v3.2'
    });

    //check user session and refresh it
    FB.getLoginStatus((response) => {
        if (response.status === 'connected') {
            //user is authorized
            document.getElementById('loginBtn').style.display = 'none';
            document.getElementById('logoutBtn').style.display = 'block';
            connectAPI(response);
        } else {

        }
    });
};

//add event listener to login button
document.getElementById('loginBtn').addEventListener('click', () => {
    //do the login
    FB.login((response) => {
        if (response.authResponse) {
            //user just authorized your app
            document.getElementById('loginBtn').style.display = 'none';

        }
    }, {
        scope: 'email', //===========change
        return_scopes: true,
        auth_type: 'rerequest'
    });
}, false);

document.getElementById('logoutBtn').addEventListener('click', () => {
    FB.logout((response) => {
        if(response.status === 'unknown'){
            document.getElementById('loginBtn').style.display = 'block';
            $('.btn-logout').hide();
        }
    });
}, false);

// Load the SDK asynchronously
(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

//successful log in
function connectAPI(respo) {
    console.log(respo);
}

输出如下:

enter image description here

但是我希望输出是这样的:

photo from google https://www.google.com/url?sa=i&source=images&cd=&cad=rja&uact=8&ved=2ahUKEwjvpcWIj6bgAhXLQN4KHUCLAN0QjRx6BAgBEAU&url=https%3A%2F%2Fwww.studytutorial.in%2Fphp-facebook-login-and-integration-in-php-website-tutorial&psig=AOvVaw1FWjb0gj6NdviN0cqfzhsk&ust=1549508480183839

我尝试更改此行:

scope: 'email'

收件人:

scope: 'email,name,picture,birthday,address,email,gender,location',

但是弹出窗口的显示如下:

enter image description here

我应该在哪里正确插入我的范围参数?

1 个答案:

答案 0 :(得分:0)

没有名为“名称”或“图片”的权限,请阅读:https://developers.facebook.com/docs/facebook-login/permissions

此外,没有address权限,而location权限实际上称为user_locationbirthdaygender相同。尝试使用正确的密码,不要重复使用email

scope: 'email,public_profile,user_birthday,user_gender,user_location'

不仅要创建权限,API参考还会准确告诉您存在哪些权限;)