Reputation: 237
I am trying to get the users email when the user is already logged in. I have been able to start a new activity when the user is already logged in but can't seem to get email or name of the user. Heres my code for starting activity when user is already logged in.
AccessToken accessToken = AccessToken.getCurrentAccessToken();
// If already logged in,start new activity
if (accessToken != null)
{
Intent intent = new Intent(LoginActivity.this,HomePage.class);
startActivity(intent);
}
Upvotes: 1
Views: 201
Reputation: 276
Make sure you asked for email on login:
...logInWithReadPermissions(this, Arrays.asList("email"));
With your current AccessToken create a GraphRequest:
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.d("jsonresponse", object.toString());
}
});
You can specify which fields do you want in the json response:
Bundle parameters = new Bundle();
parameters.putString("fields", "first_name, id, email");
request.setParameters(parameters);
request.executeAsync();
Searching on the Facebook docs I found the Profile class only returns basic info like name, last name and picture. Maybe this is the way to get the email again.
Hope it works.
Upvotes: 2