我正在尝试使用Facebook(sdk 4.0)登录我的Android应用程序后获取用户的电子邮件地址。我读过很多帖子都在问同样的事情,但我仍然无法让它发挥作用。我只需使用代码
登录用户LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends", "email"));
然后我用
创建Graph API请求LoginManager.getInstance().registerCallback(fbCallbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
System.out.println(loginResult.getAccessToken().toString());
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
System.out.println(object.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "email");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
输出只是
{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[public_profile, contact_email, user_friends, email, basic_info]}
{"id":"xxxxxxxxxxxxxxx"}
即使我删除了“字段”部分,我也会获得一个包含用户公开信息的JSON,但从不会收到电子邮件字段。我正在我自己的Facebook帐户上测试这个,我确实有一个与之关联的电子邮件地址。
答案 0 :(得分:13)
您可以按如下方式获取已记录的用户电子邮件,但请注意,
他们不保证您会收到电子邮件地址read here。
在某些情况下,虽然用户提供了电子邮件,但如果电子邮件无效,则不会通过请求。
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest( AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,GraphResponse response) {
try {
String email=object.getString("email");
Log.d(TAG + "user email ", email);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
request.executeAsync();
}
答案 1 :(得分:12)
这是完整的代码,通过它我可以从facebook获得所需的所有数据
login_facebook_button = (LoginButton) findViewById(R.id.login_facebook_button);
login_facebook_button.setReadPermissions(Arrays.asList("public_profile", "user_friends", "email", "user_birthday"));
//LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
// Callback registration
login_facebook_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.e("onSuccess", "--------" + loginResult.getAccessToken());
Log.e("Token", "--------" + loginResult.getAccessToken().getToken());
Log.e("Permision", "--------" + loginResult.getRecentlyGrantedPermissions());
Profile profile = Profile.getCurrentProfile();
Log.e("ProfileDataNameF", "--" + profile.getFirstName());
Log.e("ProfileDataNameL", "--" + profile.getLastName());
Log.e("Image URI", "--" + profile.getLinkUri());
Log.e("OnGraph", "------------------------");
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
Log.e("GraphResponse", "-------------" + response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,gender,birthday,email");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
日志中的呼叫响应是:
{Response: responseCode: 200, graphObject: {"id":"896337040431723","name":"Aneh Thakur","link":"https:\/\/www.facebook.com\/app_scoped_user_id\/896337040431723\/","gender":"male","birthday":"08\/05\/1992","email":"anehkumar@gmail.com"}, error: null}
希望这会对你有所帮助。
答案 2 :(得分:1)
fbLoginButton.setReadPermissions("public_profile", "user_friends", "user_photos", "email", "user_birthday", "public_profile", "contact_email");
“contact_email”是电子邮件权限,我错过了当我添加它给我的东西工作。