我正在使用Android项目从Facebook应用程序中获取简单数据,因此我使用facebookSDK使用此代码:
private void FetchFacebookData()
{
try
{
List<String> permissions = new ArrayList<String>();
permissions.add("user_birthday");
permissions.add("user_location");
permissions.add("email");
// start Facebook Login
Session.openActiveSession(this, true, permissions, new Session.StatusCallback()
{
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception)
{
if (session.isOpened())
{
Request.newMeRequest(session, new Request.GraphUserCallback()
{
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response)
{
if (user != null)
{
try
{
JSONObject json=user.getInnerJSONObject();
String UserID=json.getString("id");
String FirstName=json.getString("first_name");
String LastName=json.getString("last_name");
String Gender=json.getString("gender");
String Email=json.getString("email");
String UserBirthday = user.getLocation()+"";
String Location=user.getLocation()+"";
StringBuilder sb=new StringBuilder();
sb.append("User ID: " + UserID + "\n");
sb.append("First Name: " + FirstName + "\n");
sb.append("Last Name: " + LastName + "\n");
sb.append("Gender: " + Gender + "\n");
sb.append("Email: " + Email + "\n");
sb.append("User Birthday: " + UserBirthday + "\n");
sb.append("Location: " + Location + "\n");
tv.setText(sb.toString());
}
catch (Exception e)
{
}
}
}
}).executeAsync();
}
}
});
}
catch(Exception ex)
{
Log.e("Error1", ex.toString());
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
try
{
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
catch(Exception ex)
{
Log.e("Error2", ex.toString());
}
}
结果是:
如果我使用我在Facebook上创建应用程序的同一帐户登录facebook应用程序,则会获取生日和位置信息。但是,如果我使用任何其他Facebook帐户登录,则获取null。