繁体   English   中英

如何从Facebook API版本v2.4获取用户电子邮件和生日

[英]How to get user email and birthday from Facebook API Version v2.4

我无法获得API版本v2.4中的facebook用户电子邮件。 我在api version2.3中有一个应用程序,它返回用户的电子邮件和其他详细信息,但现在Facebook将api版本更新为新应用程序。 如果我使用veriosn <= 2.3之类的旧应用程序,则通过使用以下代码返回用户电子邮件。 但是在api版本2.4中,我无法获取电子邮件和生日。

GraphRequest.newMeRequest(result.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
    {
        @Override
        public void onCompleted(JSONObject me, GraphResponse response)
        {
            if (response.getError() != null)
            {
                // handle error
            }
            else
            {
                Log.e("",""+me.toString());
                Log.e("",""+response.getJSONObject().toString());

            }
        }
    }).executeAsync();

您必须明确定义要与请求一起检索的字段,例如/me?fields=id,name,email,birthday而不只是/me

看到

为了尝试改善移动网络上的性能,v2.4中的“节点和边缘”要求您显式请求GET请求所需的字段。 例如,默认情况下,GET /v2.4/me/feed不再包含喜欢和评论,但是GET /v2.4/me/feed?fields=comments,喜欢将返回数据。 有关更多详细信息,请参阅有关如何请求特定字段的文档。

从v2.4开始,您需要在请求中包括所需的字段。

GraphRequest request = GraphRequest.newMeRequest(
                     AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                          @Override
                          public void onCompleted(JSONObject userMe, GraphResponse response) {
                              if(userMe!=null){
                                 //...... do your things 

                              }
                          }
                }); 

Bundle parameters = new Bundle();

//Add the fields that you need, you dont forget add the right permission
  parameters.putString("fields","email,id,name,picture,birthday");
  request.setParameters(parameters);

//Now you can execute
             GraphRequest.executeBatchAsync(request);

这是对我有用的代码块:

GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            final JSONObject jsonObject = response.getJSONObject();
                            try{
                                email = jsonObject.getString("email");
                                first_name = jsonObject.getString("first_name");
                                last_name = jsonObject.getString("last_name");
                                gender = jsonObject.getString("gender");
                                birthday = jsonObject.getString("birthday");
                            } catch (JSONException e){
                                e.printStackTrace();
                            }
                        }
                    });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "email,first_name,last_name,gender,birthday");
            request.setParameters(parameters);
            request.executeAsync();

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM