在我的Android应用程序中,我已经完成了Facebook集成.Facebook登录工作正常。如何从android集成的Facebook集成中获取用户的电子邮件ID? 请帮忙。 提前谢谢。
答案 0 :(得分:1)
使用权限和此代码
loginButton.setReadPermissions(Arrays.asList( "email","user_birthday"));
并使用Graph Api
private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
progressDialog.dismiss();
// App code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.e("response: ", response + "");
Log.e("object: ", object + "");
try {
userfbData = new UserfbData();
userfbData.facebookID = object.getString("id").toString();
userfbData.email = object.getString("email").toString();
userfbData.name = object.getString("name").toString();
userfbData.gender = object.getString("gender").toString();
userfbData.birthday=object.getString("birthday").toString();
PrefUtils.setCurrentUser(userfbData,LoginActivity.this);
Log.d("flist","flist"+ userfbData.flist);
}catch (Exception e){
e.printStackTrace();
}
// Toast.makeText(LoginActivity.this,"welcome "+userfbData.name+userfbData.email+userfbData.gender+userfbData.birthday, Toast.LENGTH_LONG).show();
Toast.makeText(LoginActivity.this,"welcome "+ userfbData.name, Toast.LENGTH_LONG).show();
Intent intent=new Intent(LoginActivity.this,LogoutActivity.class);
startActivity(intent);
finish();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
progressDialog.dismiss();
}
@Override
public void onError(FacebookException e) {
progressDialog.dismiss();
Log.d("error","error"+e);
e.printStackTrace();
}
};
UserFbData
public class UserfbData {
public String name;
public String email;
public String facebookID;
public String gender;
public String birthday;
}
答案 1 :(得分:0)
您需要进行GraphRequest调用以获取已登录用户的个人信息,例如ID,姓名,电子邮件等。
GraphRequest request = GraphRequest.newMeRequest(accessToken,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
String email = object.getString("email");
} catch (JSONException e) {
}
}
}
);
Bundle parameters = new Bundle();
parameters.putString("fields","email");
request.setParameters(parameters);
request.executeAsync();