0

我正在尝试使用最新的 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,它会显示我的电子邮件地址以及生日和性别。下面是它的截图

GraphAPI 资源管理器的快照

4

2 回答 2

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();

        }

    }

完成。!!

于 2015-07-29T11:54:33.043 回答
0

试试这个

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

于 2015-07-27T11:32:19.113 回答