[英]Can't get Email address through Facebook Android SDK
I tried to use below code to get my facebook email address via my app, but return value is null. 我尝试使用以下代码通过我的应用获取我的Facebook电子邮件地址,但返回值为null。
I don't know what the reason, but I can get email address using Graph API explorer
. 我不知道是什么原因,但是我可以使用Graph API explorer
获取电子邮件地址。 Is there anybody know what is wrong with this code? 有人知道这段代码有什么问题吗?
@Override
public void onCreate(Bundle savedInstanceState) {
// Request email address
GraphRequest.newMeRequest(
loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
public void onCompleted(JSONObject me, GraphResponse response) {
Log.d("TEST", response.toString());
if (response.getError() != null) {
// handle error
} else {
Log.d("TEST", me.toString());
String email = me.optString("email");
String id = me.optString("id");
Log.d("TEST", "Email:" + email);
}
}
}).executeAsync();
LoginButton authButton =(LoginButton) findViewById(R.id.login_button);
List<String> permissions = new ArrayList<>();
permissions.add("public_profile");
permissions.add("email");
authButton.setReadPermissions(permissions);
}
Finally I found the solution, using newMeRequest method fetches fields from a user object. 最后,我找到了解决方案,使用newMeRequest方法从用户对象获取字段。 In the case, I need email as an additional fields, so added “email” into fields parameter and request specific fields: 在这种情况下,我需要电子邮件作为其他字段,因此在字段参数中添加了“电子邮件”并请求特定的字段:
GraphRequest request = GraphRequest.newMeRequest(
//...
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
Set the read permissions before you make the request and do it in the following way: 发出请求之前,请设置读取权限,并通过以下方式进行操作:
GraphRequestAsyncTask request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject user, GraphResponse response) {
final Profile profile = Profile.getCurrentProfile();
if ((user != null) && (profile != null)) {
accessToken = AccessToken.getCurrentAccessToken();
if (accessToken.getDeclinedPermissions().isEmpty()) {
try {
String email = user.get("email").toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}).executeAsync();
You have to set permissions before the request. 您必须在请求之前设置权限。
Anywayt the email
field may return null for two reasons: 无论如何, email
字段可能会返回null,原因有两个:
1 - user didn't confirm the email address during the registration 1-用户在注册过程中未确认电子邮件地址
2 - users could be signed up in Facebook using a phone number instead of email. 2-可以使用电话号码而不是电子邮件在Facebook中注册用户。
Documentation says: 文档说:
Note, even if you request the email permission it is not guaranteed you will get an email address. 请注意,即使您请求电子邮件许可,也不能保证您将获得一个电子邮件地址。 For example, if someone signed up for Facebook with a phone number instead of an email address, the email field may be empty. 例如,如果某人使用电话号码而不是电子邮件地址注册了Facebook,则电子邮件字段可能为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.