即使设置了权限并且字段中的信息正确,Android Facebook native 应用程序也无法获取用户电子邮件和生日

标签 android facebook facebook-android-sdk

我正在尝试获取用户电子邮件和生日(使用“我”请求)。我的应用程序具有电子邮件和 user_birthday 权限,并且我正在使用的 Facebook 帐户具有电子邮件和生日设置。

当我尝试使用“Graph API Explorer”( https://developers.facebook.com/tools/explorer ) 并使用用户 access_token 和“fields=id,email,birthday”时,我得到的只是 ID。

有人可以给我建议吗?

这是我的代码:

        Session.openActiveSession(this, true, new Session.StatusCallback() {
        // callback when session changes state
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {
                // make request to the /me API
                Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            // here I check the infos I got.
                            Log.d("some tag", user.getInnerJSONObject().toString()); // it shows only the id, no email or birthday.

                            if (Session.getActiveSession() != null) {
                                Log.i(TAG, "Log out from FB");
                                Session.getActiveSession().closeAndClearTokenInformation();
                            }
                        }
                    }
                });
                // set params.
                request.getParameters().putString("fields", "id,email,birthday");
                // execute request.
                request.executeAsync();
                Log.d(TAG, request.toString());
            }

            if (exception != null) {
                exception.printStackTrace();
            }
        }
    });

非常感谢。

最佳答案

我最终改变了使用 OpenRequest 而不是 getMeRequest 的方法:

这是我的代码:

    public void onClick(View v) {
    Logger.d(TAG, "Start FB session");
    // check if a session exists.
    Session currentSession = Session.getActiveSession();
    if (currentSession == null || currentSession.getState().isClosed()) {
        // create a new session.
        Session session = new Session.Builder(getApplicationContext()).build();
        // set it a the active session.
        Session.setActiveSession(session);
        // keep a variable link to session.
        currentSession = session;
    }
    // if a session is already open then issue a request using the available
    // session. Otherwise ask for user credentials.
    if (currentSession.isOpened()) {
        // The user is logged in.
        Logger.e(TAG, "User is logged in.");
        Session.getActiveSession().closeAndClearTokenInformation();
        Logger.i(TAG, "Session closed an token information cleared.");

        // get user data here with no extra call.
        Session.openActiveSession(this, true, new Session.StatusCallback() {
            @Override
            public void call(final Session session, SessionState state, Exception exception) {
                if (session.isOpened()) {
                    Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                        @Override
                        public void onCompleted(GraphUser user, Response response) {
                            if (user != null) {
                                Logger.i(TAG, "User:" + user.getInnerJSONObject());
                            }
                        }
                    });
                }
            }
        });
    } else {
        // Ask for username and password
        OpenRequest op = new Session.OpenRequest((Activity) this);
        // don't use SSO.
        op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
        // no callback needed.
        op.setCallback(null);
        // set permissions.
        List<String> permissions = new ArrayList<String>();
        permissions.add("email");
        permissions.add("user_birthday");
        op.setPermissions(permissions);
        // open session for read.
        currentSession.openForRead(op);
        Logger.d(TAG, "Session open for read request issued.");
    }
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Logger.d(TAG, "Request code is: " + requestCode);
    Logger.d(TAG, "Result code is: " + resultCode);
    super.onActivityResult(requestCode, resultCode, data);
    if (Session.getActiveSession() != null)
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);

    Session currentSession = Session.getActiveSession();
    if (currentSession == null || currentSession.getState().isClosed()) {
        Session session = new Session.Builder(getApplicationContext()).build();
        Session.setActiveSession(session);
        currentSession = session;
    }

    if (currentSession.isOpened()) {
        Session.openActiveSession(this, true, new Session.StatusCallback() {
            @Override
            public void call(final Session session, SessionState state, Exception exception) {
                if (session.isOpened()) {
                    Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                        @Override
                        public void onCompleted(GraphUser user, Response response) {
                            if (user != null) {
                                Logger.i(TAG, "User:" + user.getInnerJSONObject());
                            }
                        }
                    });
                }
            }
        });
    }
}

我希望这会有所帮助。

关于即使设置了权限并且字段中的信息正确,Android Facebook native 应用程序也无法获取用户电子邮件和生日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17678109/

相关文章:

Gatsby js/React js 中的 Facebook 像素

java - 为什么在 Facebook YearClass 中声明非线程 - volatile 参数

java - IntelliJ IDEA 12 重复类 R.java 和 BuildConfig.java

android - braintree 集成导致 Android 问题

android - 如何在 Android Studio 布局编辑器中使用自定义分辨率?

android - 从 firebase null 对象引用 firebase 数据库获取所有表值

java - Android中如何添加user_photos权限获取facebook相册照片?

android - 为什么我的 facebook android sdk 登录被调用了两次?

android - Android Studio 中的 Facebook sdk 导致找不到资源错误

java - 为什么 Account Kit 不再通过短信向我的 Android 应用程序发送验证码?