Tomino
Tomino

Reputation: 113

Android Facebook SDK 4.x, dont get email

I read full documentation about new facebook sdk, still dont get it how to obtain email from login accessToken. Some guys on web say's this work, for me not. What is wrong with this code and why i can not get a email. And yes i have email in facebook profile.

    // facebook login
    btnFacebook.setReadPermissions("email");
    btnFacebook.setFragment(this);
    btnFacebook.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            GraphRequest.newMeRequest(
                    loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject me, GraphResponse response) {
                            if (response.getError() != null) {
                                // handle error
                            } else {
                                String email = me.optString("email");
                                String id = me.optString("id");
                                // send email and id to your web server
                            }
                        }
                    }).executeAsync();
...

I will get token, but only with my name and id :/

CODE IS OK, GraphRequest must be explicite configuration

Upvotes: 2

Views: 757

Answers (1)

Harvi Sirja
Harvi Sirja

Reputation: 2492

try with this code for get user detail.

loginButton = (LoginButton) findViewById(R.id.login_button);

List < String > permissionNeeds = Arrays.asList("user_photos", "email",
	"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {@Override
	public void onSuccess(LoginResult loginResult) {

		System.out.println("onSuccess");

		String accessToken = loginResult.getAccessToken()
			.getToken();
		Log.i("accessToken", accessToken);

		GraphRequest request = GraphRequest.newMeRequest(
		loginResult.getAccessToken(),
		new GraphRequest.GraphJSONObjectCallback() {@Override
			public void onCompleted(JSONObject object,
			GraphResponse response) {
				Log.i("LoginActivity", response.toString());
				try {
					id = object.getString("id");
					try {
						URL profile_pic = new URL(
							"http://graph.facebook.com/" + id + "/picture?type=large");
						Log.i("profile_pic",
						profile_pic + "");

					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
					String name = object.getString("name");
					String email = object.getString("email");
					String gender = object.getString("gender");
					String birthday = object.getString("birthday");
				} 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() {
		System.out.println("onCancel");
	}

	@Override
	public void onError(FacebookException exception) {
		System.out.println("onError");
		Log.v("LoginActivity", exception.getCause().toString());
	}
});

Upvotes: 1

Related Questions