我正在尝试在登录我的应用时保存Facebook用户的电子邮件。解析提供的唯一示例就是这个,但即使我要求正确的facebook权限,它也不会保存电子邮件。
private void onLoginButtonClicked() {
if (loginProgressDialog != null) {
loginProgressDialog.dismiss();
loginProgressDialog = null;
}
loginProgressDialog = ProgressDialog.show(LoginActivity.this, "", "Logging in...", true);
List<String> permissions = Arrays.asList("public_profile", "user_about_me", "user_relationships", "user_birthday", "user_location", "email");
ParseFacebookUtils.logIn(permissions, this, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException err) {
if (loginProgressDialog != null) {
loginProgressDialog.dismiss();
loginProgressDialog = null;
}
if (user == null) {
Log.d("The Bar App", "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d("The Bar App", "User signed up and logged in through Facebook!");
showNextActivity();
} else {
Log.d("The Bar App", "User logged in through Facebook!");
showNextActivity();
}
}
});
}
是否有Facebook对象或某些JSON返回,我可以从中获取电子邮件?
答案 0 :(得分:4)
您已经要求获得权限,现在您必须实际要求提供数据。
你需要为/我做一个图表查询。
https://developers.facebook.com/docs/android/graph
有一种称为executeMeRequestAsync()
的方法,您需要尝试查看getProperty("email")
电子邮件,看看他们是否已共享该信息,请确保您处理当它是空的时候。
答案 1 :(得分:3)
最终代码供将来参考。
private void onLoginButtonClicked() {
if (loginProgressDialog != null) {
loginProgressDialog.dismiss();
loginProgressDialog = null;
}
loginProgressDialog = ProgressDialog.show(LoginActivity.this, "", "Logging in...", true);
List<String> permissions = Arrays.asList("public_profile", "user_about_me", "user_relationships", "user_birthday", "user_location", "email");
ParseFacebookUtils.logIn(permissions, this, new LogInCallback() {
@Override
public void done(ParseUser pUser, ParseException err) {
if (email != null) {
pUser.put("email", email);
}
if (fName != null) {
pUser.put("firstName", fName);
}
if (lName != null) {
pUser.put("lastName", lName);
}
if (birthday != null) {
pUser.put("birthday", birthday);
}
if (relationship != null) {
pUser.put("relationship", relationship);
}
pUser.saveInBackground();
if (loginProgressDialog != null) {
loginProgressDialog.dismiss();
loginProgressDialog = null;
}
if (pUser == null) {
Log.d("The Bar App", "Uh oh. The user cancelled the Facebook login.");
} else if (pUser.isNew()) {
Log.d("The Bar App", "User signed up and logged in through Facebook!");
showNextActivity();
} else {
Log.d("The Bar App", "User logged in through Facebook!");
showNextActivity();
}
}
});
com.facebook.Request.executeMeRequestAsync(ParseFacebookUtils.getSession(), new com.facebook.Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
email = user.getProperty("email").toString();
fName = user.getFirstName();
lName = user.getLastName();
birthday = user.getBirthday();
relationship = user.getProperty("relationship_status").toString();
}
});
}
答案 2 :(得分:1)
以下代码是我在完成一些解决方案后最终得到的。我要感谢@marialena和@mziemer的详细解决方案。
//facebook button click
pbLogin.setVisibility(View.VISIBLE);
ParseFacebookUtils.logInWithReadPermissionsInBackground(this, permissions, new LogInCallback() {
@Override
public void done(final ParseUser user, ParseException err) {
if (user == null) {
Log.d(TAG, "The user cancelled the Facebook login.");
Toast.makeText(getApplicationContext(), "Logout from Facebook and try again.", Toast.LENGTH_SHORT).show();
ParseUser.logOut();
pbLogin.setVisibility(View.GONE);
} else if (user.isNew()) {
Log.d(TAG, "User signed up and logged in through Facebook!");
getFacebookUserDetails(true, user, err);
} else {
getFacebookUserDetails(false, user, err);
}
}
});
public void getFacebookUserDetails(final boolean firstTime, final ParseUser user, final ParseException error) {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
try {
userName = object.getString("name");
userId = object.getString("id");
userGender = object.getString("gender");
userProfileURL = object.getString("link");
userEmail = object.getString("email");
firstName = object.getString("first_name");
lastName = object.getString("last_name");
JSONObject ageJsonObject = object.getJSONObject("age_range");
userAgeRangeMin = ageJsonObject.getString("min");
JSONObject picObject = object.getJSONObject("picture");
JSONObject dataObject = picObject.getJSONObject("data");
userPhoto = dataObject.getString("url");
Log.d(TAG, userName);
Log.d(TAG, userEmail);
Log.d(TAG, firstName);
//Storing userInfo in SharedPreferences
SharedPreferenceHelper.setSharedPreferenceString(getApplicationContext(), "firstName", firstName);
SharedPreferenceHelper.setSharedPreferenceString(getApplicationContext(), "lastName", lastName);
SharedPreferenceHelper.setSharedPreferenceString(getApplicationContext(), "userName", userName);
SharedPreferenceHelper.setSharedPreferenceString(getApplicationContext(), "userPhoto", userPhoto);
SharedPreferenceHelper.setSharedPreferenceString(getApplicationContext(), "userId", userId);
SharedPreferenceHelper.setSharedPreferenceString(getApplicationContext(), "userGender", userGender);
SharedPreferenceHelper.setSharedPreferenceString(getApplicationContext(), "userProfileURL", userProfileURL);
SharedPreferenceHelper.setSharedPreferenceString(getApplicationContext(), "userEmail", userEmail);
SharedPreferenceHelper.setSharedPreferenceString(getApplicationContext(), "userAgeRangeMin", userAgeRangeMin);
} catch (JSONException e) {
e.printStackTrace();
}
//User logged in with facebook for the first time
if (firstTime) {
if (!ParseFacebookUtils.isLinked(user)) {
ParseFacebookUtils.linkWithReadPermissionsInBackground(user, BlurredLoginActivity.this, permissions, new SaveCallback() {
@Override
public void done(ParseException ex) {
if (ParseFacebookUtils.isLinked(user)) {
Log.d(TAG, "Woohoo, user logged in with Facebook!");
user.setUsername(userEmail);
user.setEmail(userEmail);
user.put("FirstName", firstName);
user.put("LastName", lastName);
user.put("FullName", firstName + " " + lastName);
user.signUpInBackground(new SignUpCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
// Verifying parse configuration. This is method is for developers only.
ParseUtils.verifyParseConfiguration(BlurredLoginActivity.this);
ParseUtils.subscribeWithUsername(userEmail);
Intent successIntent = new Intent(BlurredLoginActivity.this, OurServicesActivity.class);
startActivity(successIntent);
overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
finish();
} else
Log.e(TAG, e.getMessage());
}
});
}
}
});
} else {
user.setUsername(userEmail);
user.put("FirstName", firstName);
user.put("LastName", lastName);
user.put("FullName", firstName + " " + lastName);
user.setEmail(userEmail);
user.saveInBackground();
ParseUtils.verifyParseConfiguration(BlurredLoginActivity.this);
ParseUtils.subscribeWithUsername(userEmail);
Toast.makeText(getApplicationContext(), "You can change your personal data in Settings tab!", Toast.LENGTH_SHORT).show();
Intent facebookIntent = new Intent(BlurredLoginActivity.this, OurServicesActivity.class);
startActivity(facebookIntent);
overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
}
pbLogin.setVisibility(View.GONE);
} else {
Log.d(TAG, "User logged in through Facebook!");
if (!ParseFacebookUtils.isLinked(user)) {
ParseFacebookUtils.linkWithReadPermissionsInBackground(user, BlurredLoginActivity.this, permissions, new SaveCallback() {
@Override
public void done(ParseException ex) {
if (ParseFacebookUtils.isLinked(user)) {
Log.d(TAG, "Woohoo, user logged in with Facebook!");
user.setUsername(userEmail);
user.setEmail(userEmail);
user.put("FirstName", firstName);
user.put("LastName", lastName);
user.put("FullName", firstName + " " + lastName);
user.saveInBackground();
ParseUtils.verifyParseConfiguration(BlurredLoginActivity.this);
ParseUtils.subscribeWithUsername(userEmail);
Intent facebookIntent = new Intent(BlurredLoginActivity.this, OurServicesActivity.class);
startActivity(facebookIntent);
overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
pbLogin.setVisibility(View.GONE);
}
}
});
} else {
user.setUsername(userEmail);
user.setEmail(userEmail);
user.put("FirstName", firstName);
user.put("LastName", lastName);
user.put("FullName", firstName + " " + lastName);
user.saveInBackground();
ParseUtils.verifyParseConfiguration(BlurredLoginActivity.this);
ParseUtils.subscribeWithUsername(userEmail);
Intent facebookIntent = new Intent(BlurredLoginActivity.this, OurServicesActivity.class);
startActivity(facebookIntent);
overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
pbLogin.setVisibility(View.GONE);
}
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "age_range,gender,name,id,link,email,picture.type(large),first_name,last_name");
request.setParameters(parameters);
request.executeAsync();
}
我仍然没有遇到过这个代码的情况。当我遇到它们时会更新。