Reputation: 2881
I know there is quite a lot of question about that topic already but I tried many of them without having succeed in retrieving my own email address.
I'd like to know if I did something wrong and/or there is some obvious solution to the problem I am facing.
So here is how I get the Auth Token via Parse SDK (on Android):
List<String> permissions = Arrays.asList("email", "public_profile");
ParseFacebookUtils.logInWithReadPermissionsInBackground(SignupActivity.this, permissions, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
builder.setTitle("Fail");
builder.setMessage("Looks like a cancellation");
builder.show();
} else {
makeMeRequest();
}
}
});
At the beginning, I naively thought that doing a simple ParseUser.getCurrentUser().getEmail()
would have be enough to get the email but it wasn't (quite fascinating when you know that Parse belongs to Facebook). But then, I decided to create a Me request to Graph API directly. It looks like this:
private void makeMeRequest() {
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
if (jsonObject != null) {
JSONObject userProfile = new JSONObject();
Log.d(TAG, jsonObject.toString());
try {
ParseUser currentUser = ParseUser.getCurrentUser();
userProfile.put("facebookId", jsonObject.getLong("id"));
userProfile.put("name", jsonObject.getString("name"));
if (jsonObject.has("gender"))
userProfile.put("gender", jsonObject.getString("gender"));
if (jsonObject.has("email"))
currentUser.setEmail(jsonObject.getString("email"));
// Save the user profile info in a user property
currentUser.put("profile", userProfile);
currentUser.saveInBackground();
AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
builder.setTitle("Success");
builder.setMessage("Logged in with Facebook!\nDetails:\n" + currentUser.getEmail());
builder.show();
Log.d("MyApp", "User signed up and logged in through Facebook!");
} catch (JSONException e) {
Log.d(TAG,
"Error parsing returned user data. " + e);
}
} else if (graphResponse.getError() != null) {
switch (graphResponse.getError().getCategory()) {
case LOGIN_RECOVERABLE:
Log.d(TAG,
"Authentication error: " + graphResponse.getError());
break;
case TRANSIENT:
Log.d(TAG,
"Transient error. Try again. " + graphResponse.getError());
break;
case OTHER:
Log.d(TAG,
"Some other error: " + graphResponse.getError());
break;
}
}
}
});
request.executeAsync();
}
As some good observers could have noticed, that's not my code it comes from another thread/github/gist (made too much tries, cannot remember!). The problem with that is that it returns me:
{"name":"Laurent Meyer","id":"10205968929559630"}
which is pretty nice (better than nothing), but not my email address.
The question is then: did I do some mistake on my code or the problem come from my Facebook settings (another cause could be that I'm using a test version of my FB app)?
Upvotes: 0
Views: 187
Reputation: 476
By default, GraphRequest.newMeRequest
returns the default fields id
and name
. To get other fields, you need to specify the required fields before your call to request.executeAsync()
:
Bundle params = new Bundle();
params.putString("fields", "name,id,email");
request.setParameters(params);
Upvotes: 1