无法通过Facebook Android SDK获取电子邮件地址
我试图使用下面的代码通过我的应用程序获取我的facebook电子邮件地址,但返回值为空 我不知道原因是什么,但我可以使用Graph API explorer获取电子邮件地址。有人知道这个代码有什么问题吗无法通过Facebook Android SDK获取电子邮件地址,android,facebook,email,Android,Facebook,Email,我试图使用下面的代码通过我的应用程序获取我的facebook电子邮件地址,但返回值为空 我不知道原因是什么,但我可以使用Graph API explorer获取电子邮件地址。有人知道这个代码有什么问题吗 @Override public void onCreate(Bundle savedInstanceState) { // Request email address GraphRequest.newMeRequest( loginResult.getAccessToken(),
@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);
}
在发出请求之前设置读取权限,并按以下方式执行:
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();
在发出请求之前设置读取权限,并按以下方式执行:
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();
您必须在请求之前设置权限 无论如何,电子邮件字段可能返回null,原因有两个: 1-用户在注册期间未确认电子邮件地址 2-用户可以使用电话号码而不是电子邮件在Facebook上注册 文件说: 注意,即使您请求电子邮件许可,也不能保证 你会得到一个电子邮件地址。例如,如果有人注册了 用电话号码代替电子邮件地址的Facebook 字段可能为空
您必须在请求之前设置权限 无论如何,电子邮件字段可能返回null,原因有两个: 1-用户在注册期间未确认电子邮件地址 2-用户可以使用电话号码而不是电子邮件在Facebook上注册 文件说: 注意,即使您请求电子邮件许可,也不能保证 你会得到一个电子邮件地址。例如,如果有人注册了 用电话号码代替电子邮件地址的Facebook 字段可能为空
最后我找到了解决方案,使用newMeRequest方法从用户对象获取字段。在这种情况下,我需要电子邮件作为附加字段,因此在字段参数和请求特定字段中添加了“电子邮件”:
GraphRequest request = GraphRequest.newMeRequest(
//...
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
最后我找到了解决方案,使用newMeRequest方法从用户对象获取字段。在这种情况下,我需要电子邮件作为附加字段,因此在字段参数和请求特定字段中添加了“电子邮件”:
GraphRequest request = GraphRequest.newMeRequest(
//...
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
谢谢你的回复。谢谢你的回复。我的情况并非如此。在我看来,某些应用程序权限配置不正确,我当前的应用程序处于开发模式,在facebook开发者网页中,登录权限中的状态显示电子邮件权限不可供所有用户使用,因为该应用程序不是实时的。感谢您的回复。感谢您的回复。我的情况并非如此。在我看来,某些应用程序权限配置不正确,我当前的应用程序处于开发模式,在facebook开发者网页中,登录权限中的状态显示电子邮件权限不可供所有用户使用,因为该应用程序不是实时的。我无法获取电子邮件,除了此技巧,,,,电子邮件正在工作,为什么拒绝投票,,,除了这个小把戏,我没法拿到邮件,邮件有效,为什么要投否决票,,,