我使用以下代码从Facebook获取用户的电子邮件和姓名
protected void onCreate(Bundle savedInstanceState) {
//Display page
setContentView(R.layout.activity_main);
AppEventsLogger.activateApp(MainActivity.this);
FBloginButton = (Button) findViewById(R.id.fb_login_button);
callbackManager = CallbackManager.Factory.create();
FBloginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Login the user through facebook when button is clicked
onFblogin();
}
});
}
private void onFblogin() {
callbackManager = CallbackManager.Factory.create();
// Set permissions
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_friends", "public_profile"));
//Try logging in the user
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.i("LoginActivity", response.toString());
// Get facebook data from login
try {
email = object.getString("email");
} catch (JSONException e) {
e.printStackTrace();
}
try {
name = object.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
//new fbLogin().execute();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "email, name");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
});
}
但是,这只返回用户的姓名而不是电子邮件。我怎样才能得到两者?
答案 0 :(得分:3)
我的工作方法如下:
public void getUserDetailsFromFB(AccessToken accessToken) {
GraphRequest req=GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Toast.makeText(getApplicationContext(),"graph request completed",Toast.LENGTH_SHORT).show();
try{
String email = object.getString("email");
String birthday = object.getString("birthday");
String gender = object.getString("gender");
String name = object.getString("name");
String id = object.getString("id");
String photourl =object.getJSONObject("picture").getJSONObject("data").getString("url");
}catch (JSONException e)
{
Toast.makeText(getApplicationContext(),"graph request error : "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday,picture.type(large)");
req.setParameters(parameters);
req.executeAsync();
}
然后在你的LoginButton中:
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
getUserDetailsFromFB(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(),"fb user canceled",Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException e) {
Toast.makeText(getApplicationContext(),"fb error",Toast.LENGTH_SHORT).show();
}
});
初始化:
LoginButton loginButton =(LoginButton)findViewById(R.id.facebook_login_button);
<com.facebook.login.widget.LoginButton
android:id="@+id/facebook_login_button"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
facebook:com_facebook_login_text="Fb login button"
android:layout_gravity="center_horizontal|bottom"
android:textSize="20dp"
android:layout_centerHorizontal="true" />