正在获取用户'通过Facebook API庆祝生日

Getting user's birthday through Facebook API

本文关键字:Facebook 通过 API 庆祝 祝生日 获取 用户      更新时间:2023-11-27

我在使用Facebook API时遇到了一些问题,我正在通过Javascript:请求用户的某些信息的权限

FB.login(function (response) {
    if (response.authResponse) {
        // authorized
    } else {
        // canceled
    }
}, {scope: 'email,user_birthday,user_location'});

现在我想通过PHP:获得用户的信息(当他被授权时)

$facebookUser = json_decode(file_get_contents('https://graph.facebook.com/' . $this->data['userid'] . '?access_token=' . $this->oathtoken . '&fields=id,name,location,gender,email,picture,birthday,link'));

一切都很好,除了API没有给用户回生日。即使我在公共场合设置了生日隐私设置,它也不会出现。

因此:

echo $facebookUser['birthday']; // Gives an error, since this key does not exists

有人知道怎么解决这个问题吗?

您在客户端登录,但php在服务器端执行,因此您只收到用户对象的基本信息。

如果你在服务器端登录并拥有权限,那么你只需使用就可以获得生日

echo $facebookUser['birthday'];

很可能您没有收到FB请求user_birthday的批准。

除非FB审查你的FB应用程序,否则你无法获得user_birthday

仅可请求以下权限而无需审查:public_profileuser_friendsemail(请参见:https://developers.facebook.com/docs/facebook-login/review/what-is-login-review)

无需审查,您只能请求age_range,而不能请求user_birthday

FB.login(function (response) {
    var user_birthday = response.user_birthday; 
    console.log(user_birthday);
if (response.authResponse) {
} else {
    // canceled
}
}, {scope: 'email,user_birthday,user_location'});

如果你想在服务器端获取FB信息,为什么不使用服务器端身份验证呢

如果您可以接受客户端身份验证,那么上面的操作应该可以。