Facebook Android SDK 4.0没有返回emailID

时间:2015-07-27 11:26:08

标签: android facebook facebook-graph-api facebook-android-sdk facebook-graph-api-v2.4

我正在尝试使用最新的facebook SDK 4.0实现Facebook登录。它只返回用户ID和用户名。下面是我使用GRAPH API实现的代码。

@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
        textView = (TextView) view.findViewById(R.id.textView);

        List<String> permissions = new ArrayList<>();
        //permissions.add("user_friends");
        permissions.add("email");
        //permissions.add("user_birthday");
        loginButton.setReadPermissions(permissions);
        loginButton.setFragment(this);
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {

                            @Override
                            public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                                if (graphResponse.getError() != null) {
                                    // handle error
                                } else {
                                    String email = jsonObject.optString("email");
                                    String id = jsonObject.optString("id");
                                    new GraphRequest(
                                            AccessToken.getCurrentAccessToken(),
                                            "/" + id,
                                            null,
                                            HttpMethod.GET,
                                            new GraphRequest.Callback() {
                                                @Override
                                                public void onCompleted( GraphResponse graphResponse) {
                                                        Log.d("FACEBOOK LOGIN","graph response" +graphResponse.toString());
                                                }
                                            }
                                    ).executeAsync();
                                    Log.d("FACEBOOK LOGIN ","email id and the user_id of the looged in user is = ::" + email+" ::"+id+"" +
                                            "JsonObject from facebook i am getting is :" +jsonObject.toString());
                                    // send email and id to your web server
                                }

                            }

                        }).executeAsync();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException e) {

            }
        });

    }

此外,我尝试使用GRAPH API EXPLORER,它向我显示我的电子邮件地址以及生日和性别。下面是它的快照

Snapshot of GraphAPI explorer

2 个答案:

答案 0 :(得分:2)

试试这个。

    String username,email ;

    loginButton.registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        new fblogin().execute(loginResult.getAccessToken());

                    }

                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onError(FacebookException e) {

                    }
                });




class fblogin extends AsyncTask<AccessToken, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Login.this);
            pDialog.setMessage("Loading Data..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(AccessToken... params) {
            GraphRequest request = GraphRequest.newMeRequest(params[0],
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object,
                                GraphResponse response) {

                            try {

                                username = object.getString("first_name");

                                emailid = object.getString("email");


                            } catch (JSONException e) {
                                // TODO Auto-generated catch
                                // block
                                e.printStackTrace();
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields",
                    "id,first_name,email,gender,birthday");
            request.setParameters(parameters);
            request.executeAndWait();

            return null;
        }

        protected void onPostExecute(String file_url) {

            pDialog.dismiss();

        }

    }

它完成了。!!

答案 1 :(得分:0)

尝试这个

loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends", "email"));