1

Any idea, why I cannot get email from Facebook? I got error like - No value for "email" or sth like that.

    private void fbInit() {
    // Initialize the SDK before executing any other operations,
    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);
    callbackManager = CallbackManager.Factory.create();

    // get new instance of fb
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    loginResult.getAccessToken().getUserId();

                    getFbData(loginResult.getAccessToken().getUserId());
                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onError(FacebookException error) {

                }
            });

    // access token tracker init
    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(
                AccessToken oldAccessToken,
                AccessToken currentAccessToken) {
            // Set the access token using
            // currentAccessToken when it's loaded or set.
            accessToken = currentAccessToken;
            updateWithToken(currentAccessToken);
        }
    };

    // If the access token is available already assign it.
    accessToken = AccessToken.getCurrentAccessToken();
    updateWithToken(accessToken);

    // is this need?!
//        profileTracker = new ProfileTracker() {
//            @Override
//            protected void onCurrentProfileChanged(
//                    Profile oldProfile,
//                    Profile currentProfile) {
//                // App code
//            }
//        };
}

private void updateWithToken(AccessToken currentAccessToken) {
        // this way I check if there is email permission which is needed to register/login
        if (currentAccessToken != null
                && !currentAccessToken.getDeclinedPermissions().contains("email")
                && !currentAccessToken.getDeclinedPermissions().contains("basic_info")) {
            // go to other activity (?)
        } else {
            ActivityUtils.showToast(this, "no email permission");
        }
    }

private void getFbPermissions() {
    Collection<String> permissions = Arrays.asList("public_profile", "email", "user_photos", "user_birthday");
    LoginManager.getInstance().logInWithReadPermissions(this, permissions);
}

private void getFbData(String userId) {
    GraphRequest request = GraphRequest.newMeRequest(
            AccessToken.getCurrentAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    if (response != null) {
                        try {
                            Log.i("LoginActivityFbResponse", response.toString());
                            ActivityUtils.showToast(getActivity(),
                                    object.getString("id") + object.getString("name") + object.getString("email")
                                            + object.getJSONArray("location").getString(1) + object.getString("locale")
                                            + object.getString("birthday") + object.getString("gender"));

                            // TODO: try to login
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id, name, email");
    request.setParameters(parameters);
    request.executeAsync();
}

BTW. I want to get from Facebook at least this data: email, id, gender, birthday, location_name.

y07k2
  • 1,898
  • 4
  • 20
  • 36

1 Answers1

0

You get Error with birthday because you dont have permission of birthday in your GraphRequest. Use it like :

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

Can you try this and come with result ?

Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
  • I have this: `parameters.putString("fields", "id, name, email, gender, birthday, picture.type(large)");` after I read comments in this post. But still only birthday doesn't have value with error: `No value for birthday`... – y07k2 Jun 10 '16 at 07:20