javascript 通过 Facebook API 获取用户的生日

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11536382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:34:54  来源:igfitidea点击:

Getting user's birthday through Facebook API

phpjavascriptfacebook

提问by Lumocra

I'm having some troubles with the Facebook API, I am requesting permission for some information of the user by Javascript:

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

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

Now I want to get the user's information (when he is authorized) by PHP:

现在我想通过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'));

All working great, except that the API is not giving the user's birthday back. Even when I set my birthday privacy settings on public it won't show up.

一切都很好,除了 API 没有返回用户的生日。即使我将生日隐私设置设为公开,它也不会显示。

So:

所以:

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

Does somebody have an idea how to fix this?

有人知道如何解决这个问题吗?

回答by Besnik

You are logged in on the client side but php is executed on the server side so you receive only the basic information of an user object.

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

If you logged in on the server side and have the permissionsso you can get the birthday simply with:

如果您在服务器端登录并拥有权限,那么您可以简单地通过以下方式获得生日:

echo $facebookUser['birthday'];

回答by Dima L.

Most probably you didn't receive approval to request user_birthdayfrom FB.

很可能你没有得到user_birthdayFB 的批准。

You can't get user_birthdayunless FB reviews your FB app.

user_birthday除非 FB 您的 FB 应用程序,否则您无法获得。

Only the following permissions may be requested without review: public_profile, user_friendsand email(See: https://developers.facebook.com/docs/facebook-login/review/what-is-login-review)

可要求只有以下权限,而不回顾:public_profileuser_friendsemail(参见:https://developers.facebook.com/docs/facebook-login/review/what-is-login-review

Without review you can request only age_range, but not user_birthday

未经,您只能请求age_range,但不能user_birthday

回答by Seho Lee

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

why don't you use serverside auth if you would like get FB info in server side?

if you are ok with clientside authentication, above should work.

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

如果您对客户端身份验证没问题,上面应该可以工作。