在Android中,Facebook API V2.4没有返回电子邮件ID,而V2.3正在返回。如何在V2.4中获取电子邮件ID?

时间:2015-07-23 13:51:08

标签: android facebook email

当我为API V2.3编写以下代码时,这给了我所有详细信息,包括电子邮件ID。现在相同的代码没有给我电子邮件ID。我可以做些什么来获取电子邮件ID?

    oncreate(..)
    {
    .
    .    
    EMAIL_PERMISSION = new ArrayList<String>();
    EMAIL_PERMISSION.add("email");
    uiLifecycleHelper = new UiLifecycleHelper(this, statusCallback);

    uiLifecycleHelper.onCreate(savedInstanceState);

   Session.openActiveSession(this, true, EMAIL_PERMISSION,   
                                          statusCallback);
    // callback when session changes state
Session.StatusCallback statusCallback = new StatusCallback()
{
    @Override
    public void call(Session session, SessionState state, Exception  
                                                         exception)
    {

        // Checking whether the session is opened or not
        if (state.isOpened())
        {
        } else
        {
            if (state.isClosed())
            {
            }
            Log.d(TAG, state.toString());
        }
    }
};
 // Method to get user facebook profile
void getUserFacebookProfile(Session session, final boolean finish)
{
    // Checking whether the session is opened or not
    if (session.isOpened())
    {
        // Sending request to the facebook to get user facebook profile
        Request.newMeRequest(session, new GraphUserCallback()
        {

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

                if (user != null)
                {
                    // To get network user id
                    String networkUserid = user.getId();
                    // To get user first name
                    String fname = user.getFirstName();
                    // To get user last name
                    String lname = user.getLastName();
                    // To get user middle name
                    String mname = user.getMiddleName();
                // String email = user.getProperty("email").toString();
    String email = response.getGraphObject().getProperty("email")
                     .toString();
                }

现在上面的代码给了我所有细节,包括V2.3的电子邮件ID,现在我无法获得电子邮件ID。请让我知道解决方案。感谢。

2 个答案:

答案 0 :(得分:0)

public class LoginFacebook {
    CallbackManager callbackManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();


    public void openFB() {
            LoginManager.getInstance().logInWithReadPermissions(activity,
                    Arrays.asList("read_stream", "user_photos", "email", "user_location"));

            // Login Callback registration
            LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

                @Override
                public void onSuccess(final LoginResult loginResult) {
                    new GraphRequest(AccessToken.getCurrentAccessToken(),
                            "/me", null, HttpMethod.GET,
                            new GraphRequest.Callback() {
                                public void onCompleted(
                                        GraphResponse response) {
                                    /* handle the result */

                                    try {
                                        //GET USER INFORMATION
                                        JSONObject json = response.getJSONObject();
                                        String email = json.getString("email");
                                        String fullName = json.getString("name");
                                        String accessToken = loginResult.getAccessToken().getToken();
                                        int type = 1;
                                        String lastUpdate = json.getString("updated_time");
                                        String user_id = json.getString("id");

                                    } catch (JSONException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            }).executeAsync();

                    GetSnsPost getSnsPost = GetSnsPost.getInstance(activity);
                    getSnsPost.getFacebookPosts();

                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onError(FacebookException exception) {

                }
            });

        }

     public void loginFacebook(View v){
           openFB();
     }
     protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);

    }
    }

由于会话早已被弃用,我不再使用它了。我以这种方式获得用户信息。希望这段代码能解决你的问题;)

答案 1 :(得分:-1)

Bundle params = new Bundle(); params.putString(“fields”,“id,name,email,birthday,first_name,last_name”);

新的GraphRequest(                                 AccessToken.getCurrentAccessToken()                                 AccessToken.getCurrentAccessToken()。getUserId()                                 params,HttpMethod.GET,                                 新的GraphRequest.Callback(){

                                @Override
                                public void onCompleted(
                                        GraphResponse response) {

                                    System.out.println("\n J S O N :"
                                            + response.toString());


                                    } catch (JSONException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            }).executeAsync();