从facebook SDK获取电子邮件地址For Android

时间:2016-01-20 04:27:39

标签: java android facebook

我已在我的应用程序中集成了Facebook登录。我获取了其他个人资料信息,但无法从sdk获取电子邮件吗?

public void getProfileInformation_fb() {
        mAsyncRunner.request("me", new RequestListener() {
            @Override
            public void onComplete(String response, Object state) {

                String json = response;
                try {
                    JSONObject profile = new JSONObject(json);
                    // getting name of the user

                    final String name = profile.getString("name");
                    // getting email of the user
                    final String email = profile.getString("email");

                    final String id = profile.getString("id");


                    Log.i("ID , Name", ""+id+ ""+name);

                    Log.i("Email", ""+email);


                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Log.i("Face Book", "" + email);
                        }

                    });

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onIOException(IOException e, Object state) {
            }

            @Override
            public void onFileNotFoundException(FileNotFoundException e,
                    Object state) {
            }

            @Override
            public void onMalformedURLException(MalformedURLException e,
                    Object state) {
            }

            @Override
            public void onFacebookError(FacebookError e, Object state) {
            }
        });
    }

2 个答案:

答案 0 :(得分:2)

此代码ID适用于我。

  LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // App code
                        accessToken = loginResult.getAccessToken();


                        GraphRequest request = GraphRequest.newMeRequest(
                                accessToken,
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(
                                            JSONObject object,
                                            GraphResponse response) {
                                        // Application code

                                        try {

 String name = object.getString("name");
                    String birthday = object.getString("birthday");
                    String gender = object.getString("gender");
                    String email = object.getString("email");
                    String picture = object.getString("picture");






                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }










                                    }
                                });
                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "id,name,link,birthday,first_name,gender,last_name,location,email,picture.type(large)");
                        request.setParameters(parameters);
                        request.executeAsync();







                    }

                    @Override
                    public void onCancel() {
                        // App code
                        Log.e("fb", "cancel ");
                        LoginManager.getInstance().logOut();
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        // App code
                        Log.e("fb", "error " + exception);
                        LoginManager.getInstance().logOut();
                    }
                });

答案 1 :(得分:0)

   var rjApp = angular.module('rjApp',[]);
    rjApp.config(function($interpolateProvider) {
      $interpolateProvider.startSymbol('{::');
      $interpolateProvider.endSymbol('::}');
    })


//mainCotroller, should be work by ngClick through radio button 
    function mainController($scope, $rootScope) { 
        $scope.broadcast = function(event){
        console.log(event) //I've been thought, this is the part to passing the parameter of radio button, but not gonna works.
            $rootScope.$broadcast('handleBroadcast'); 
        };
    }


//the Directive, should be injected dinamically template, depends on ngClick parameter inside radion button  
    rjApp.directive('exampleDirective', function() {
        return {

            restrict: 'E',
            scope: {
                message: '='
            },
            link: function(scope, elm, attrs) {
                scope.$on('handleBroadcast', function(doifq) {  
                    templateUrl= '<?php echo url("registrasi/rawat_jalan/penanggung_biaya/") ?>'+doifq //This is the part to injected the dinamically template. And I've been guess the **doifq**, is the paramter to passing by the mainController
                });                     
            },
        };
    });

}

并添加

public class FbLogin extends Activity{


CallbackManager callbackManager;


private static final String NAME = "name";
private static final String ID = "id";
private static final String PICTURE = "picture.type(large)";
private static final String EMAIL = "email";
private static final String FIELDS = "fields";

private static final String REQUEST_FIELDS =
        TextUtils.join(",", new String[]{ID, NAME, EMAIL, PICTURE});



Bitmap mIcon1 = null;
 String imgUrl = "";
 String id = "";
 String name = "";
 String email = "";




@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_type);



     callbackManager = CallbackManager.Factory.create();


    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {



            fetchUserInfo();

        }

        @Override
        public void onCancel() {


        }

        @Override
        public void onError(FacebookException e) {



        }
    });

        btnFb.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            LoginManager.getInstance().logInWithReadPermissions(Activity_LoginType.this, Arrays.asList("public_profile", "user_friends","email"));
        }
    });


}




private void fetchUserInfo() {
    final String PERMISSION = "publish_actions";
    final AccessToken accessToken = AccessToken.getCurrentAccessToken();
    if (accessToken != null) {
        GraphRequest request = GraphRequest.newMeRequest(
                accessToken, new GraphRequest.GraphJSONObjectCallback() {

                    @Override
                    public void onCompleted(JSONObject object,
                            GraphResponse response) {
                        // TODO Auto-generated method stub



                            parseUserInfo(object);



                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString(FIELDS, REQUEST_FIELDS);
        request.setParameters(parameters);
        GraphRequest.executeBatchAsync(request);

        LoginManager.getInstance().logInWithPublishPermissions(
                this,
                Arrays.asList(PERMISSION));
    } else {


    }
}

private void parseUserInfo(JSONObject me) {



    try {
        id = me.getString("id");
        name = me.getString("name");
        email = me.getString("email");
        imgUrl = me.getJSONObject("picture").getJSONObject("data").getString("url");


    } catch (JSONException e) {
        e.printStackTrace();
    }


}






@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        callbackManager.onActivityResult(requestCode, resultCode, data);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

在应用程序类中。 并在清单文件中添加以下代码

FacebookSdk.sdkInitialize(getApplicationContext());