2

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

当我尝试使用带有 'fields=id,email,birthday' 的用户 access_token 使用 'Graph API Explorer' ( https://developers.facebook.com/tools/explorer ) 时,我得到的只是 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();
            }
        }
    });

非常感谢。

4

5 回答 5

2

我终于改变了使用 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());
                            }
                        }
                    });
                }
            }
        });
    }
}

我希望这有帮助。

于 2013-09-25T15:50:50.710 回答
0

当您使用Request.GraphUserCallback()时,最好在MAIN UI THREAD中使用它

Session s = Session.getActiveSession();

    if(s!=null)
    {
     Request.executeMeRequestAsync(s,new Request.GraphUserCallback() 
     {

        @Override
        public void onCompleted(GraphUser user, Response response) 
        {


            if(user!=null)
            {
                 try {

                    String fbid=null;
                    String birthday=null;
                    fbid=user.getId();
                    birthday=user.getBirthday();


                } catch (JSONException e1) {

                    e1.printStackTrace();
                }



            }

        }
     });
    }
于 2013-08-02T05:35:33.543 回答
0

试试这个:

如果您有用户对象,请执行以下操作:

if (user != null) {
 String UserId = user.getId();
try {
Log.i("Birthdate", ""+user.getInnerJSONObject().getString("birthday"));
} catch (JSONException e) {
e.printStackTrace();
}
}
于 2014-05-01T06:36:23.103 回答
0

这段代码对我有用,希望你觉得它有用。这以 3 种不同的方式检索用户电子邮件并记录它

     try{       
    //  new Read().execute("Facebook"); 
        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.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                      if (user != null) {
                      //  TextView welcome = (TextView) findViewById(R.id.welcome);
                       // welcome.setText("Hello " + user.getName() + "!");

                        Log.d("useremai", (String) user.getProperty("email"));

                    Log.d("USER",  user.getInnerJSONObject().toString()); 
                    JSONObject details = user.getInnerJSONObject();
                    useremail = details.optString("email");
                    Log.d("name", user.getName());
                    Log.d("email", useremail);

                      }
                    }
                  });
                }
              }
            });
        }catch(NoClassDefFoundError e){
            e.printStackTrace();
        }
于 2013-11-14T10:51:39.367 回答
0

你也可以在 Request.executeMeRequestAsync(...){...}

        List<String> permissions = session.getPermissions();

        List<String> permissionsBirthday = new ArrayList<String>();
        permissionsBirthday.add("user_birthday");

        if (!isSubsetOf(permissionsBirthday, permissions)) {
            pendingPublishReauthorization = true;
            Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, permissionsBirthday);

            if (setPermissions(session, newPermissionsRequest))
                return;
        }

方法 isSubsetOf();

private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}

希望这可以帮助。

于 2013-10-01T19:08:45.990 回答