Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
956 views
in Technique[技术] by (71.8m points)

android - how to get email from Facebook sdk 4.0

I'm working on android Login with Facebook After login i want to save the data from Facebook i want to save the following data from Facebook

  1. First Name
  2. Middle Name
  3. Last Name
  4. User id
  5. profile picture
  6. Email
  7. Profile Name

I tried some code which following

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

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

        loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_friends"));

        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                AccessToken accessToken = loginResult.getAccessToken();
                Profile profile = Profile.getCurrentProfile();

                String userid = profile.getId();
                String userfirstname = profile.getFirstName();
                String middlename = profile.getMiddleName();
                String userlastname = profile.getLastName();
                Uri userimage = profile.getProfilePictureUri(30, 40);
                String name = profile.getName();
            }

            @Override
            public void onCancel() {
                //info = ("Login attempt canceled.");
            }

            @Override
            public void onError(FacebookException e) {
                // info = ("Login attempt failed.");
            }
        });

From above code i successfully get other all but can't get email.

What i want

  1. I want to get user email
  2. I want to create a new json array for HTTP CALL to save the above data from user account.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try This It's Work For me

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

            if (AccessToken.getCurrentAccessToken() != null) {
                RequestData();
            }
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException exception) {
        }
    });




private void RequestData() {

    GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject object,GraphResponse response) {

            final JSONObject json = response.getJSONObject();



            try {
                if(json != null){
                    text = "<b>Name :</b> "+json.getString("name")+"<br><br><b>Email :</b> "+json.getString("email")+"<br><br><b>Profile link :</b> "+json.getString("link");
                    /*details_txt.setText(Html.fromHtml(text));
                    profile.setProfileId(json.getString("id"));*/

                    Log.e(TAG, json.getString("name"));
                    Log.e(TAG, json.getString("email"));
                    Log.e(TAG, json.getString("id"));
                    //web.loadData(text, "text/html", "UTF-8");

                }







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


    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,link,email,picture");
    request.setParameters(parameters);
    request.executeAsync();
}

//Get Profile Picture from id

 public static Bitmap getFacebookProfilePicture(String userID){
    try {
        URL imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?type=large");
        Log.e(TAG,imageURL.toString());
        try {
            bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

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

    return bitmap;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...