android 如何使用 Facebook SDK v4.2 获取用户详细信息
How to get user details using Facebook SDK v4.2 for android
我能够使用以下代码实现 facebook 登录:
public class MainActivity extends ActionBarActivity implements FacebookCallback<LoginResult> {
//List<String> permissionNeeds=Arrays.asList("user_photos","friends_photos", "email", "user_birthday", "user_friends");
TextView t;
String get_id, get_name, get_gender, get_email, get_birthday, get_locale, get_location;
private CallbackManager mCallbackManager;
private LoginButton mFbLoginButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init facebook sdk and
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
//instantiate callbacks manager
mCallbackManager = CallbackManager.Factory.create();
mFbLoginButton=(LoginButton)findViewById(R.id.login_button);
mFbLoginButton.registerCallback(mCallbackManager, this);
mFbLoginButton.setReadPermissions(Arrays.asList("user_status","email"));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//manage login result
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
public void onSuccess(LoginResult loginResults) {
Toast.makeText(getApplicationContext(), "Success ",
Toast.LENGTH_LONG).show();
}
public void onCancel() {
}
public void onError(FacebookException e) {
}
}
现在如何获取用户详细信息,例如姓名、电子邮件、年龄等。我已经在网上搜索了几个示例,但我 unsuccessful.Where 我应该为它添加代码吗?
要做到这一点,您可以使用它。
//add variable names.
private static String uid,email;
public void getuserdata() {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
Log.v("LoginActivity", response.toString());
try {
//and fill them here like so.
uid = object.getString("id");
email = object.getString("email")
} catch (JSONException e) {
e.printStackTrace();
}
getGroups();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
例如,要使用它,只需在创建时调用 getuserdata()。
我能够使用以下代码实现 facebook 登录:
public class MainActivity extends ActionBarActivity implements FacebookCallback<LoginResult> {
//List<String> permissionNeeds=Arrays.asList("user_photos","friends_photos", "email", "user_birthday", "user_friends");
TextView t;
String get_id, get_name, get_gender, get_email, get_birthday, get_locale, get_location;
private CallbackManager mCallbackManager;
private LoginButton mFbLoginButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init facebook sdk and
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
//instantiate callbacks manager
mCallbackManager = CallbackManager.Factory.create();
mFbLoginButton=(LoginButton)findViewById(R.id.login_button);
mFbLoginButton.registerCallback(mCallbackManager, this);
mFbLoginButton.setReadPermissions(Arrays.asList("user_status","email"));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//manage login result
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
public void onSuccess(LoginResult loginResults) {
Toast.makeText(getApplicationContext(), "Success ",
Toast.LENGTH_LONG).show();
}
public void onCancel() {
}
public void onError(FacebookException e) {
}
}
现在如何获取用户详细信息,例如姓名、电子邮件、年龄等。我已经在网上搜索了几个示例,但我 unsuccessful.Where 我应该为它添加代码吗?
要做到这一点,您可以使用它。
//add variable names.
private static String uid,email;
public void getuserdata() {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
Log.v("LoginActivity", response.toString());
try {
//and fill them here like so.
uid = object.getString("id");
email = object.getString("email")
} catch (JSONException e) {
e.printStackTrace();
}
getGroups();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
例如,要使用它,只需在创建时调用 getuserdata()。