我正在尝试使用最新的 facebook SDK 4.0 实现 facebook 登录。它只返回用户 ID 和用户名。下面是我使用 GRAPH API 实现的代码。
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
textView = (TextView) view.findViewById(R.id.textView);
List<String> permissions = new ArrayList<>();
//permissions.add("user_friends");
permissions.add("email");
//permissions.add("user_birthday");
loginButton.setReadPermissions(permissions);
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest.newMeRequest(
loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
if (graphResponse.getError() != null) {
// handle error
} else {
String email = jsonObject.optString("email");
String id = jsonObject.optString("id");
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/" + id,
null,
HttpMethod.GET,
new GraphRequest.Callback() {
@Override
public void onCompleted( GraphResponse graphResponse) {
Log.d("FACEBOOK LOGIN","graph response" +graphResponse.toString());
}
}
).executeAsync();
Log.d("FACEBOOK LOGIN ","email id and the user_id of the looged in user is = ::" + email+" ::"+id+"" +
"JsonObject from facebook i am getting is :" +jsonObject.toString());
// send email and id to your web server
}
}
}).executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
});
}
我还尝试使用 GRAPH API EXPLORER,它会显示我的电子邮件地址以及生日和性别。下面是它的截图