Ashish Tikarye
Ashish Tikarye

Reputation: 870

Facebook not provide email in latest sdk 4.4.1?

i already specify 'email' permission but i cant able to get email with latest facebook sdk 4.4.1...if any one have solution then please guide me. Thanks in advance...

public void loginToFacebook() {

    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);

    if (access_token != null) {
        facebook.setAccessToken(access_token);

        btnFbLogin.setVisibility(View.INVISIBLE);

        // Making get profile button visible
        btnFbGetProfile.setVisibility(View.VISIBLE);

        // Making post to wall visible
        btnPostToWall.setVisibility(View.VISIBLE);

        // Making show access tokens button visible
        btnShowAccessTokens.setVisibility(View.VISIBLE);

        Log.d("FB Sessions", "" + facebook.isSessionValid());
    }

    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }

    if (!facebook.isSessionValid()) {
        facebook.authorize(this,
                new String[] { "email", "publish_stream" },
                new DialogListener() {

                    @Override
                    public void onCancel() {
                        // Function to handle cancel event
                    }

                    @Override
                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                facebook.getAccessToken());
                        editor.putLong("access_expires",
                                facebook.getAccessExpires());
                        editor.commit();

                        // Making Login button invisible
                        btnFbLogin.setVisibility(View.INVISIBLE);

                        // Making logout Button visible
                        btnFbGetProfile.setVisibility(View.VISIBLE);

                        // Making post to wall visible
                        btnPostToWall.setVisibility(View.VISIBLE);

                        // Making show access tokens button visible
                        btnShowAccessTokens.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onError(DialogError error) {
                        // Function to handle error

                    }

                    @Override
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors

                    }

                });
    }
}


public void getProfileInformation() {
    mAsyncRunner.request("me", new RequestListener() {
        @Override
        public void onComplete(String response, Object state) {
            Log.d("Profile", response);
            String json = response;
            try {
                // Facebook Profile JSON data
                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");

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
                    }

                });


            } 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) {
        }
    });
}

This my code but m not getting email can you plz navigate where can i put this below code.

Upvotes: 2

Views: 170

Answers (2)

Prashant
Prashant

Reputation: 1613

you need to pass fields you require with request like :

Bundle parameters = new Bundle();
parameters.putString("fields","id,name,email,gender, birthday");
GraphRequest.setParameters(parameters);
GraphRequest.executeAsync();

Upvotes: 1

andyrandy
andyrandy

Reputation: 74004

You need to specify the fields now: https://developers.facebook.com/docs/apps/changelog#v2_4 (Declarative Fields)

For example:

/me?fields=email

Upvotes: 0

Related Questions