Reputation: 55
I was able to implement the the facebook login using the code :
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) {
}
}
Now how can I get the user details such as name , email ,age ,etc .I have searched the web for a few examples but i was unsuccessful.Where am i supposed to put the code for it ?
Upvotes: 0
Views: 925
Reputation: 1801
To do this you can use this.
//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();
}
And to use this just call getuserdata() in your on create for example.
Upvotes: 1