如何获得与图请求在Facebook的Android SDK中4.0登录用户的电子邮件地址 [英] How to get the logged user's email address with Graph Request in Facebook Android sdk 4.0
问题描述
我试图获取用户的电子邮件地址,一旦他登录到我的Android应用程序与Facebook(SDK 4.0)。我看了很多帖子问同样的事情,但我还是不能使它发挥作用。我只需登录用户与code
I'm trying to get the user's email address once he's logged into my Android app with Facebook (sdk 4.0). I've read many posts asking the same thing but I still couldn't make it work. I simply log the user in with the code
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends", "email"));
然后,我让以
Then I make the Graph API request with
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账户测试这一点,我有一个电子邮件地址与其关联。
Even if I remove the "fields" part, I get a JSON with a bunch of the user's public info, but never the email field. I'm testing this on my own Facebook account, and I do have an email address associated to it.
推荐答案
您可以按如下获取登录用户的电子邮件,但需要注意的是,
You can get the logged user email as follows , But note that ,
-
他们不保证你会得到一个电子邮件地址<一个href="https://developers.facebook.com/docs/facebook-login/permissions/v2.3#reference-public_profile">read这里。
在某些情况下,虽然用户已经提供了电子邮件,它不会请求来通过,如果电子邮件是无效
In some cases, though user has provided an email, it will not come through request, if the email is not valid.
@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();
}
这篇关于如何获得与图请求在Facebook的Android SDK中4.0登录用户的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!