How to get email id From Android Facebook SDK 4.6.0?

前端 未结 3 831
野趣味
野趣味 2021-01-29 10:20

Here is my code for getting user information after facebook login. I am trying to get emailid from user I am getting Name , id , but not getting the emailid .I have tried with t

相关标签:
3条回答
  • 2021-01-29 10:41

    this way to get all part of Facebook login...

        locationMangaer = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        loginButton = (LoginButton)findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList("email", "user_photos", "public_profile", "user_friends"));
    
        callbackManager= CallbackManager.Factory.create();
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {
                                // Application code
                                Log.e("LoginActivity", response.toString() + "    " + object.toString());
    
                                try {
                                    String name=object.getString("name");
                                    String email=object.getString("email");
                                    String id=object.getString("id");
                                    String gender=object.getString("gender");
    
    
    
    
                                    Log.e("LoginActivity", name + "    " + email + "   " + id + "   " + gender);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
    
                            }
                        });
    
    
    
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();
    
            }
    
            @Override
            public void onCancel() {
                // App code
                Log.e("LOGIN Cancle", "LOGIN Cancle");
            }
    
            @Override
            public void onError(FacebookException exception) {
                // App code
                Log.e("LOGIN Cancle", exception.getMessage());
            }
        });
    
    0 讨论(0)
  • 2021-01-29 10:43

    Starting with Graph API v2.4, you need to specifiy each field which you want to have returned in your query.

    new GraphRequest(AccessToken.getCurrentAccessToken(),
      "/me", null , HttpMethod.GET,
      new GraphRequest.Callback() {
        ...
    

    needs to become

    new GraphRequest(AccessToken.getCurrentAccessToken(),
      "/me?fields=id,name,email", null , HttpMethod.GET,
      new GraphRequest.Callback() {
        ...
    

    This is very well documented, and there are dozens of similar questions here on SO. Please refer to the docs first before posting a question.

    Keep in mind that you need the appropriate permissions to be able to request user details. In this case it's email

    See:

    • https://developers.facebook.com/docs/apps/changelog/#v2_4
    • https://developers.facebook.com/docs/facebook-login/permissions/v2.5
    0 讨论(0)
  • 2021-01-29 10:50
    LoginManager.getInstance().logInWithReadPermissions(WelcomeActivity1.this, (Arrays.asList("public_profile", "user_friends","user_birthday","user_about_me","email")));
    
    String email;
    
    
    LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
     @Override
      public void onSuccess(LoginResult loginResult) {
      Log.d("tag","FF fb onSuccess");
      GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject object,GraphResponse response) {
         try {
               String[] splited ;
               JSONObject obj =  object.getJSONObject("picture").getJSONObject("data");
    
                                  if (object.has("email"))
                                  {
                                     email =  object.getString("email");
                                  }
                                  else
                                  {
                                      email = "";
                                  }
    
    
    
                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
    
    
                                }
                            });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,link,birthday,picture,email,gender");
                    request.setParameters(parameters);
                    request.executeAsync();
    
    
                }
    
                @Override
                public void onCancel() {
                    Log.d("tag","fb onCancel");
                     // App code
                }
    
    
    
    @Override
                public void onError(FacebookException exception) {
                    Log.d("tag","fb onError");
                     // App code   
                }
    });
    
    0 讨论(0)
提交回复
热议问题