You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android集成Facebook登录无法获取邮箱、生日等用户信息求助

问题分析与解决方案

看起来你遇到的问题主要出在权限请求格式错误和Graph API字段未正确指定这两个关键点上,我帮你一步步修正:

1. 修复权限请求的语法错误

你的权限列表里把user_birthday和user_gender放在了同一个字符串里,这会导致Facebook无法识别这两个权限。需要把它们拆分成单独的列表项:

fb_login.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_gender"));

2. 补充Graph API请求的字段

你当前的parameters.putString("fields", "id,name,link,gender");只请求了基础字段,没有包含email、birthday和头像信息,这就是你拿不到这些数据的核心原因。我们需要把这些字段加进去,同时可以直接通过picture字段指定尺寸获取头像,不用自己拼接URL:

Bundle parameters = new Bundle();
// 加入email、birthday,以及指定尺寸的picture字段
parameters.putString("fields", "id,name,link,gender,email,birthday,picture.height(250).width(250)");
request.setParameters(parameters);
request.executeAsync();

3. 修正getDate方法的逻辑

现在可以从返回的JSONObject里直接获取头像URL,同时要处理字段可能为空的情况(比如用户未公开邮箱或生日,或者权限未通过):

private void getDate(JSONObject object){
    try {
        // 从picture字段获取头像URL
        JSONObject pictureObj = object.getJSONObject("picture").getJSONObject("data");
        String profilePicUrl = pictureObj.getString("url");
        Picasso.with(this).load(profilePicUrl).into(image);

        id.setText(object.getString("id"));
        name.setText(object.getString("name"));
        
        // 处理可能为空的字段,避免JSONException
        if(object.has("email")){
            email.setText(object.getString("email"));
        } else {
            email.setText("未获取到邮箱");
        }
        
        if(object.has("gender")){
            gende.setText(object.getString("gender")); // 建议把变量名改成genderTv,更规范易读
        } else {
            gende.setText("未获取到性别");
        }
        
        if(object.has("birthday")){
            birthday.setText(object.getString("birthday"));
        } else {
            birthday.setText("未获取到生日");
        }
        
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

额外注意事项

  • 权限审核:user_birthday这类非公开权限需要你在Facebook开发者平台提交应用审核,审核通过后才能在正式环境中获取。测试阶段可以使用Facebook的测试用户来验证功能。
  • 用户隐私设置:即使用户授权了权限,如果用户在Facebook的隐私设置中隐藏了邮箱或生日,你依然无法获取到这些数据,所以一定要做好空值处理。

内容的提问来源于stack exchange,提问作者mahesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:54:45