java – Android Facebook SDK 4.X,如何获取电子邮件地址和Facebook访问令牌将其传递给Web服务

编辑:
我的问题是如何通过Facebook SDK 4.X获取电子邮件,UserId,Facebook身份验证,此刻,与Ming Respond,我知道如何获取电子邮件,用户ID,所以我的问题是如何从会话后获取Facebook身份验证GraphUser刚刚被LoginManager和AccessToken所替代,并没有关于它的信息?

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import java.util.Arrays;

public class RegisterActivity extends Activity {
    private String fbUserID;
    private String fbProfileName;
    private String fbAuthToken;
    private LoginButton fbLoginBtn;
    private static final String TAG = "FacebookLogin";
    CallbackManager callbackManager;
    private AccessTokenTracker accessTokenTracker;
    private ProfileTracker profileTracker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);
        fbLoginBtn = (LoginButton) findViewById(R.id.connect_with_facebook_button);
        fbLoginBtn.setReadPermissions(Arrays.asList("email", "user_photos", "public_profile"));
        fbLoginBtn.setBackgroundResource(R.drawable.connect_facebook_button);


        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(
                    AccessToken oldAccessToken,
                    AccessToken currentAccessToken) {
                fbAuthToken = currentAccessToken.getToken();
                fbUserID = currentAccessToken.getUserId();


                Log.d(TAG, "User id: " + fbUserID);
                Log.d(TAG, "Access token is: " + fbAuthToken);


            }
        };

        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(
                    Profile oldProfile,
                    Profile currentProfile) {
                fbProfileName = currentProfile.getName();

                Log.d(TAG, "User name: " + fbProfileName );
            }
        };


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

            }

            @Override
            public void onCancel() {
                // App code
            }

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

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);



    }

GraphRequest request = GraphRequest.newMeRequest(
        accessToken,
        new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(
                    JSONObject user,
                    GraphResponse response) {
                String id = user.optString("id");
                String firstName = user.optString("first_name");
                String lastName = user.optString("last_name");
                String email = user.optString("email");

            }

    @Override
    public void onSaveInstanceState(Bundle savedState) {
        super.onSaveInstanceState(savedState);
    }
最佳答案
fbLoginBtn.registerCallback(callbackManager, 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();
    }

    @Override
    public void onCancel() {
        // App code
    }

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

转载注明原文:java – Android Facebook SDK 4.X,如何获取电子邮件地址和Facebook访问令牌将其传递给Web服务 - 代码日志