Facebook Android SDK 4.5.0 get email address

后端 未结 7 1528
[愿得一人]
[愿得一人] 2020-12-04 16:02

I\'m currently creating a test application to test using the latest facebook SDK to update our existing application problem is that I need to get the email address which I k

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 16:22

    Hope it helps you

    /*** setupFacebook stuff like make login with facebook and get the userId,Name and Email*/
        private void setupFacebookStuff() {
            Log.e(TAG, "key hash= " + Utils.getKeyHash(SplashActivity.this, getPackageName()));
    
            // This should normally be on your application class
            FacebookSdk.sdkInitialize(SplashActivity.this);
    
            accessTokenTracker = new AccessTokenTracker() {
                @Override
                protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                       currentAccessToken.getToken();
                }
            };
    
            loginManager = LoginManager.getInstance();
            callbackManager = CallbackManager.Factory.create();
    
            LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback() {
                @Override
                public void onSuccess(LoginResult loginResult) {
    
                    preference.setUserLogin(true);
    
                    final GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            try {
                                    Log.e("id", "" + object);
    
                                if (object.has(getString(R.string.fbParamId))) {
    
                                    final String userId = object.optString(getString(R.string.fbParamId));
                                    final String userPicture = "https://graph.facebook.com/" + object.optString(getString(R.string.fbParamId)) + "/picture?type=large";
    
                                    preference.setUserId(userId);
                                    preference.setUserPictureUrl(userPicture);
    
                                }
                                if (object.has(getString(R.string.fbParamUserName))) {
    
    
                                    final String userName = object.optString(getString(R.string.fbParamUserName));
                                    preference.setUserName(userName);
                                }
    
                                if (object.has(getString(R.string.fbParamEmail))) {
    
                                    final String userEmail = object.optString(getString(R.string.fbParamEmail));
                                    preference.setUserName(userEmail);
                                    Log.e("useremail", userEmail);
                                }
    
                                callMainActivity(true);
    
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
    
                    final Bundle parameters = new Bundle();
                    parameters.putString("fields", "name,email,id");
                    request.setParameters(parameters);
                    request.executeAsync();
                }
    
                @Override
                public void onCancel() {
                    Toast.makeText(getBaseContext(), "Login Cancelled", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onError(FacebookException error) {
                    Toast.makeText(getBaseContext(), "Problem connecting to Facebook", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "Facebook login error " + error);
                }
            });
        }
    

提交回复
热议问题