这是我登录facebook后获取用户信息的代码。 我正在尝试从用户获取emailid我得到了Name,id,但没有收到emailid。我尝试使用Login Button和Login Manager类都给出了相同的结果。 现在如何从响应中获取电子邮件ID:
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
new GraphRequest(AccessToken.getCurrentAccessToken(),
"/me", null , HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(
GraphResponse response) {
handle the result
if (response !=null )
{
//GET USER INFORMATION
Toast.makeText(getApplicationContext(),"Hello in JSON",Toast.LENGTH_LONG).show();
JSONObject json = response.getJSONObject();
//JSONArray jsona = response.getJSONArray();
Log.i(TAG,"" +json);
// Log.i(TAG,"" +json);
String email = json.optString("email");
// String email = json.optString("email");
//Log.i("email", "" + email);
Log.i(TAG,"" +email);
String fullName = json.optString("name");
String location = json.optString("location");
String accessToken = loginResult.getAccessToken().getToken();
String user_id = json.optString("id");
Log.i(TAG,"" +json);
Toast.makeText(getApplicationContext(),fullName,Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(),email,Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(),user_id,Toast.LENGTH_LONG).show();
//int type = 1;
// String lastUpdate = json.getString("updated_time");
// Log.i("email", "" + email);
// Log.i("Message", "HELLO");
Log.i("Name", "" + fullName);
Log.i("ID", "" + user_id);
}
}
}).executeAsync();
谢谢.. !!
答案 0 :(得分:1)
LoginManager.getInstance().logInWithReadPermissions(WelcomeActivity1.this, (Arrays.asList("public_profile", "user_friends","user_birthday","user_about_me","email")));
String email;
LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("tag","FF fb onSuccess");
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,GraphResponse response) {
try {
String[] splited ;
JSONObject obj = object.getJSONObject("picture").getJSONObject("data");
if (object.has("email"))
{
email = object.getString("email");
}
else
{
email = "";
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,birthday,picture,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Log.d("tag","fb onCancel");
// App code
}
@Override
public void onError(FacebookException exception) {
Log.d("tag","fb onError");
// App code
}
});
答案 1 :(得分:1)
通过这种方式获得Facebook登录的所有部分......
locationMangaer = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("email", "user_photos", "public_profile", "user_friends"));
callbackManager= CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
Log.e("LoginActivity", response.toString() + " " + object.toString());
try {
String name=object.getString("name");
String email=object.getString("email");
String id=object.getString("id");
String gender=object.getString("gender");
Log.e("LoginActivity", name + " " + email + " " + id + " " + gender);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
// App code
Log.e("LOGIN Cancle", "LOGIN Cancle");
}
@Override
public void onError(FacebookException exception) {
// App code
Log.e("LOGIN Cancle", exception.getMessage());
}
});
答案 2 :(得分:0)
从Graph API v2.4开始,您需要指定要在查询中返回的每个字段。
new GraphRequest(AccessToken.getCurrentAccessToken(),
"/me", null , HttpMethod.GET,
new GraphRequest.Callback() {
...
需要成为
new GraphRequest(AccessToken.getCurrentAccessToken(),
"/me?fields=id,name,email", null , HttpMethod.GET,
new GraphRequest.Callback() {
...
这是非常好的记录,在这里有几十个类似的问题。在发布问题之前,请先参考文档。
请注意,您需要相应的权限才能请求用户详细信息。在这种情况下,它是email
请参阅: