获取用户的Facebook电子邮件和其他详细信息

时间:2017-04-10 14:30:38

标签: java android facebook

我想给我的用户选择使用Facebook注册。如何检索详细信息并将其提供给我的服务器以供用户注册 。我正在使用api将用户注册到我的服务器。请帮忙:

public class DashboardActivity extends AppCompatActivity {

private TextView loginView;
private LinearLayout signUpButtonParent, loginInButtonParent, signUpRoot, loginRoot;
private View loginBorder,  signUpBorder;
private Button signUpWithEmailButton, loginWithEmailButton,loginFacebook;
LoginButton signUpFacebook;
private SharedPreferenceUtil sharedPreference;


private CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    FacebookSdk.sdkInitialize(this);
    callbackManager = CallbackManager.Factory.create();

    signUpFacebook =(LoginButton) findViewById(R.id.facebookSignup);
    loginFacebook =(Button) findViewById(R.id.loginFacebook);
    signUpFacebook.setReadPermissions("email","user_photos","user_friends");

    setupViews();


}

private void setupViews() {

    final Typeface tf = Typeface.createFromAsset(getAssets(), "login_font.ttf");

    loginView.setTypeface(tf);

   signUpFacebook.setOnClickListener(view -> 
        LoginManager.getInstance().logInWithReadPermissions(DashboardActivity.this,Arrays.asList("public_profile", "email")));

    signUpFacebook.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            startActivity(new Intent(DashboardActivity.this, MainActivity.class));
        }

        @Override
        public void onCancel() {
            Toast.makeText(DashboardActivity.this, "Login was Cancelled", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(FacebookException error) {
            Toast.makeText(DashboardActivity.this, "LogIn failed", Toast.LENGTH_SHORT).show();
        }
    });

}


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




}

接口:

public interface ApiService {
  @FormUrlEncoded
@POST("signup")
Observable<StatusItem> signUp(@Field("full_name")String full_name, @Field("phone") String phone, @Field("email") String email, @Field("password") String password);

}

1 个答案:

答案 0 :(得分:0)

试试吧。

@Override
public void onSuccess(LoginResult success) {
AccessToken token = success.getAccessToken();
getFBMeData(token);
}



 public void getFBMeData(AccessToken atoken) {
    Profile.fetchProfileForCurrentAccessToken();
    GraphRequest request = GraphRequest.newMeRequest(
            AccessToken.getCurrentAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {

                @Override
                public void onCompleted(JSONObject object,
                                        GraphResponse response) {
                    // TODO Auto-generated method stub
                    if (object != null) {
                        Log.d("FBGraphJSONObject", object.toString());

                       String id =  Profile.getCurrentProfile()
                                .getId();
                       String fName = Profile.getCurrentProfile()
                                .getFirstName();
                        String lName = Profile.getCurrentProfile()
                                .getLastName();
                        String gender = object.optString("gender");
                        String dob = object.optString("birthday");
                        String locationName = "";
                        JSONObject location = object
                                .optJSONObject("location");
                        if (location != null) {
                            locationName = location.optString("name");
                        }
                        String pictureUrl = "", email = "";
                        JSONObject picture = object
                                .optJSONObject("picture");
                        JSONObject data = picture.optJSONObject("data");
                        try {
                            email = URLDecoder.decode(
                                    object.optString("email"), "UTF-8");
                            if (picture != null) {
                                pictureUrl = URLDecoder.decode(
                                        data.optString("url"), "UTF-8");
                            }
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }




                }
            });
    Bundle params = new Bundle();
    params.putString("fields", "gender,email,birthday,location,picture");
    request.setParameters(params);
    request.executeAsync();


}