Currently, I am using this tutorial to create the facebook login.
"https://github.com/ParsePlatform/ParseUI-Android"
The Parse user JSON didn't return me an email or gender. I also have research around but I still can't find the solution (well, at least, some other still returning null). Anyone can explain the reason and how to solve it?
facebookLoginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
loadingStart(false); // Facebook login pop-up already has a spinner
if (config.isFacebookLoginNeedPublishPermissions()) {
ParseFacebookUtils.logInWithPublishPermissionsInBackground(getActivity(),
Arrays.asList(
"public_profile", "email"), facebookLoginCallbackV4);
Log.i("fbUser","Publish");
} else {
ParseFacebookUtils.logInWithReadPermissionsInBackground(getActivity(),
Arrays.asList( "public_profile", "email"), facebookLoginCallbackV4);
Log.i("fbUser", "Read");
}
}
});
private LogInCallback facebookLoginCallbackV4 = new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if (isActivityDestroyed()) {
return;
}
if (user == null) {
loadingFinish();
if (e != null) {
showToast(R.string.com_parse_ui_facebook_login_failed_toast);
debugLog(getString(R.string.com_parse_ui_login_warning_facebook_login_failed) +
e.toString());
}
} else if (user.isNew()) {
GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject fbUser,
GraphResponse response) {
/*
If we were able to successfully retrieve the Facebook
user's name, let's set it on the fullName field.
*/
ParseUser parseUser = ParseUser.getCurrentUser();
if (fbUser != null && parseUser != null && fbUser.optString("name").length() > 0) {
Log.i("FbUSer", response.getJSONObject().toString());
Log.d("FbUSer-Email",fbUser.optString("email"));
Log.i("FbUSer-gender",fbUser.optString("gender"));
parseUser.put(USER_OBJECT_NAME_FIELD, fbUser.optString("name"));
parseUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null) {
debugLog(getString(
R.string.com_parse_ui_login_warning_facebook_login_user_update_failed) +
e.toString());
}
loginSuccess();
}
});
}
loginSuccess();
}
}
).executeAsync();
} else {
loginSuccess();
}
}
};
Logcat Section return:
{"id":"1234524124124","name":"FbUser NAme"}
I just found the answer. I just need to set the parameter so I can get the data I want.
GraphRequest request = GraphRequest.newMeRequest(...);
Bundle parameters = new Bundle();
parameters.putString("fields","id,name,link,email,first_name,last_name,gender");
request.setParameters(parameters);
request.executeAsync();
Reference: https://stackoverflow.com/a/32579366/4961962
for more user info you have to add permissoin to access those detail like email, user_about_me at facebook developer console
select app at facebook developer and go -> app details -> Configure
App Center Permissions
Related
i'm tryng to integrate facebook login in my android application. For now i get a new row in Parse Backend about the logged user which contains his real name, some weird value(ex:SnvvKEsIv6tX7...) in his userName, and an authData which is Json and contains the following values:
access_token, expiration date, id.
After debugging i got the url of the json response, it only contains two values, the name and the id, however i declared the list like this:
List permissions = Arrays.asList("public_profile", "email");
So for now the graph is only returnin two values, i thought it was due to the permissions but as i sayed i did declared. Here is my code:
public class LoginActivity extends Activity {
private Dialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FacebookSdk.sdkInitialize(getApplicationContext());
ParseFacebookUtils.initialize(this);
// Check if there is a currently logged in user
// and it's linked to a Facebook account.
ParseUser currentUser = ParseUser.getCurrentUser();
if ((currentUser != null) && ParseFacebookUtils.isLinked(currentUser)) {
// Go to the user info activity
showUserDetailsActivity();
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
}
public void onLoginClick(View v) {
progressDialog = ProgressDialog.show(LoginActivity.this, "", "Logging in...", true);
List<String> permissions = Arrays.asList("public_profile", "email");
// NOTE: for extended permissions, like "user_about_me", your app must be reviewed by the Facebook team
// (https://developers.facebook.com/docs/facebook-login/permissions/)
ParseFacebookUtils.logInWithReadPermissionsInBackground(this, permissions, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
progressDialog.dismiss();
if (user == null) {
Log.d("TAG", "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject fbUser,
GraphResponse response) {
/*
* If we were able to successfully retrieve the
* Facebook user's name, let's set it on the
* fullName field.
*/
Log.e("facebook User", fbUser.toString());
final ParseUser parseUser = ParseUser
.getCurrentUser();
if (fbUser != null
&& parseUser != null
&& fbUser.optString("name").length() > 0) {
parseUser.put("name",
fbUser.optString("name"));
parseUser.put("email",
fbUser.optString("email"));
Log.v("isnew, values: ","values : "+fbUser.optString("name")+","+fbUser.optString("email")+","+fbUser.optString("age_range")+","+fbUser.optString("locale"));
parseUser
.saveInBackground(new SaveCallback() {
#Override
public void done(
ParseException e) {
if (e != null) {
Log.v("", (getString(R.string.com_parse_ui_login_warning_facebook_login_user_update_failed)
+ e.toString()));
}
ParseInstallation installation = ParseInstallation
.getCurrentInstallation();
installation
.put(ParseConstants.KEY_USER_ID,
parseUser
.getUsername());
installation
.saveInBackground();
showUserDetailsActivity();
}
});
}
}
}).executeAsync();
} else {
Log.d("TAG", "User not new logged in through Facebook!");
showUserDetailsActivity();
}
}
});
}
private void showUserDetailsActivity() {
Intent intent = new Intent(this, UserDetailsActivity.class);
startActivity(intent);
}
}
public class UserDetailsActivity extends Activity {
private ProfilePictureView userProfilePictureView;
private TextView userNameView;
private TextView userGenderView;
private TextView userEmailView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userdetails);
userProfilePictureView = (ProfilePictureView) findViewById(R.id.userProfilePicture);
userNameView = (TextView) findViewById(R.id.userName);
userGenderView = (TextView) findViewById(R.id.userGender);
userEmailView = (TextView) findViewById(R.id.userEmail);
//Fetch Facebook user info if it is logged
ParseUser currentUser = ParseUser.getCurrentUser();
if ((currentUser != null) && currentUser.isAuthenticated()) {
makeMeRequest();
}
}
#Override
public void onResume() {
super.onResume();
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
// Check if the user is currently logged
// and show any cached content
updateViewsWithProfileInfo();
} else {
// If the user is not logged in, go to the
// activity showing the login view.
startLoginActivity();
}
}
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();
try {
userProfile.put("facebookId", jsonObject.getLong("id"));
userProfile.put("name", jsonObject.getString("name"));
if (jsonObject.getString("gender") != null)
userProfile.put("gender", jsonObject.getString("gender"));
if (jsonObject.getString("email") != null)
userProfile.put("email", jsonObject.getString("email"));
// Save the user profile info in a user property
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put("profile", userProfile);
currentUser.saveInBackground();
// Show the user info
updateViewsWithProfileInfo();
} 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();
}
private void updateViewsWithProfileInfo() {
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser.has("profile")) {
JSONObject userProfile = currentUser.getJSONObject("profile");
try {
if (userProfile.has("facebookId")) {
userProfilePictureView.setProfileId(userProfile.getString("facebookId"));
} else {
// Show the default, blank user profile picture
userProfilePictureView.setProfileId(null);
}
if (userProfile.has("name")) {
userNameView.setText(userProfile.getString("name"));
} else {
userNameView.setText("");
}
if (userProfile.has("gender")) {
userGenderView.setText(userProfile.getString("gender"));
} else {
userGenderView.setText("");
}
if (userProfile.has("email")) {
userEmailView.setText(userProfile.getString("email"));
} else {
userEmailView.setText("");
}
} catch (JSONException e) {
Log.d("TAG", "Error parsing saved user data.");
}
}
}
public void onLogoutClick(View v) {
logout();
}
private void logout() {
// Log the user out
ParseUser.logOut();
// Go to the login view
startLoginActivity();
}
private void startLoginActivity() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
i'm using Parse 1.9.4 and ParseFacebookUtils 1.9.4.
for my android application i wanted to integrate a facebook login, so i did exactly what is here:
https://github.com/ParsePlatform/IntegratingFacebookTutorial
Everything seems to work fine however the values which are stored in the parse backend are not convenient, here is for example what i get after login with my facebook account:
emailVerified:empty
username:adGdJMxcCQCAo2.....
authData: {"facebook":{"access_token":"CAAMBDxU3HgUBAMYQ8q2mnjDqeKBz2sw......","expiration_date":"...","id":"1015353..."}}
Sorry i still can't upload pictures..
Is this normal?
i'm using parseFacebookUtils 1.9.4
parse 1.9.4
This is what i had done for facebook parse.com login
LoginFragment.java
private ParseLoginConfig config;
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState, 1);
rootView = inflater.inflate(R.layout.layout_login, container, false);
config = ParseLoginConfig.fromBundle(getArguments(), getActivity());
return rootView;
}
private void loginUsingFacebook() {
// TODO Auto-generated method stub
if (config.isFacebookLoginNeedPublishPermissions()) {
ParseFacebookUtils.logInWithPublishPermissionsInBackground(
getActivity(), Arrays.asList("public_profile", "email"),
facebookLoginCallbackV4);
} else {
ParseFacebookUtils.logInWithReadPermissionsInBackground(
getActivity(), Arrays.asList("public_profile", "email"),
facebookLoginCallbackV4);
}
}
private LogInCallback facebookLoginCallbackV4 = new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if (isActivityDestroyed()) {
return;
}
if (user == null) {
loadingFinish();
if (e != null) {
showToast(R.string.com_parse_ui_facebook_login_failed_toast);
debugLog(getString(R.string.com_parse_ui_login_warning_facebook_login_failed)
+ e.toString());
}
} else if (user.isNew()) {
GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject fbUser,
GraphResponse response) {
/*
* If we were able to successfully retrieve the
* Facebook user's name, let's set it on the
* fullName field.
*/
Log.e("facebook User", fbUser.toString());
final ParseUser parseUser = ParseUser
.getCurrentUser();
if (fbUser != null
&& parseUser != null
&& fbUser.optString("name").length() > 0) {
parseUser.put(USER_OBJECT_NAME_FIELD,
fbUser.optString("name"));
parseUser.put(USER_OBJECT_EMAIL_FIELD,
fbUser.optString("email"));
parseUser
.saveInBackground(new SaveCallback() {
#Override
public void done(
ParseException e) {
if (e != null) {
debugLog(getString(R.string.com_parse_ui_login_warning_facebook_login_user_update_failed)
+ e.toString());
}
ParseInstallation installation = ParseInstallation
.getCurrentInstallation();
installation
.put(BaseFragment.INSTALLATION_UNIQUE_ID,
parseUser
.getUsername());
installation
.saveInBackground();
loginSuccess();
}
});
}
}
}).executeAsync();
}
}
};
Note : Remove toast messages and and text me if any query!!
I am working on an application using Parse.com as backend support.I have used ParseUser for login and signup ,but now I have to implement change Password and forgot password ,but don't know how to implement it..Please help me to implement this functionalities.
The code which I have used to login the ParseUser is as follows:
ParseUser.logInInBackground(str_email2, str_password2, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
dlg.dismiss();
if(e == null)
{
Log.d(">>>","ObjId>>"+user.getObjectId()+" Username>>>"+user.getUsername());
}
else
loginUnSuccessful();
}
});
To request a new password (in case the user forgot it) you can use this:
ParseUser.requestPasswordResetInBackground("myemail#example.com",
new RequestPasswordResetCallback() {
public void done(ParseException e) {
if (e == null) {
// An email was successfully sent with reset instructions.
} else {
// Something went wrong. Look at the ParseException to see what's up.
}
}
});
And to change a password you can just get the current user and do the following:
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.setPassword("new_password");
currentUser.saveInBackground();
In the doc, you have this sample code:
ParseUser.requestPasswordResetInBackground("myemail#example.com",
new RequestPasswordResetCallback() {
public void done(ParseException e) {
if (e == null) {
// An email was successfully sent with reset instructions.
} else {
// Something went wrong. Look at the ParseException to see what's up.
}
}
});
Source: https://parse.com/docs/android_guide#users-resetting
You can not reset the password for not signed (anonymous) ParseUser.
If you are using email feature for ParseUser, you can send password reset mail like this
ParseUser.requestPasswordResetInBackground(
stringEmail,
new RequestPasswordResetCallback() {
public void done(ParseException e) {
...
}
}
);
If you are not using email feature, you need to write a cloud method in your main.js file, like this
Parse.Cloud.define("resetPassword", function (request, response) {
Parse.Cloud.useMasterKey(); /*important*/
var username = request.params.username;
var password = request.params.password;
var objUser = Parse.Object.extend("User");
var qUser = new Parse.Query(objUser);
qUser.equalTo("username", username);
qUser.find().then(function (resultObj) {
if(resultObj.length > 0){
var userObj = resultObj[0];
userObj.set("password", password);
return userObj.save();
} else {
response.error("User does not exist");
}
}).then(function () {
response.success("Password RESET DONE SUCCESSFULLY");
}, function (error) {
response.error("Error " + error);
});
});
and call that method from your app, like this
Map<String, Object> params = new HashMap<String, Object>();
params.put("password", password);
params.put("username", username);
ParseCloud.callFunctionInBackground("resetPassword", params, new FunctionCallback<String>() {
#Override
public void done(String response, ParseException e) {
if (e == null) {
...
} else {
..
}
}
});
This way you can reset user password using username or mobile number.
I am trying to get email address of user from ParseFacebook for android but i am getting null when i try to fetch email.
here is my code
//on Login:
List<String> permissions = Arrays.asList("email","basic_info", "user_about_me", "user_location");
ParseFacebookUtils.logIn(permissions,SignUpActivity. this, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
SignUpActivity.this.progressDialog.dismiss();
if(err!=null)
Log.w("parse exception",""+err.getMessage());
if (user == null) {
Log.e("FB","Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.w("FB","User signed up and logged in through Facebook!");
//showUserDetailsActivity();
getProfileInfo();
} else {
Log.w("FB","User logged in through Facebook!");
// showUserDetailsActivity();
getProfileInfo();
}
}
});
making request:
Request request = Request.newMeRequest(ParseFacebookUtils.getSession(),new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
JSONObject userProfile = new JSONObject();
try {
// Populate the JSON object
try {
userProfile.put("facebookId", user.getId());
userProfile.put("name", user.getName());
} catch (Exception e) {
// TODO: handle exception
}
if (user.getProperty("email") != null) {
userProfile.put("email",(String) user.getProperty("email"));
}
if (user.getLocation().getProperty("name") != null) {
userProfile.put("location", (String) user.getLocation().getProperty("name"));
}
if (user.getProperty("gender") != null) {
userProfile.put("gender",(String) user.getProperty("gender"));
}
} catch (JSONException e) {
Log.e("Facebook parsing data","Error parsing returned user data.");
}
} else if (response.getError() != null) {
if ((response.getError().getCategory() == FacebookRequestError.Category.AUTHENTICATION_RETRY) || (response.getError().getCategory() == FacebookRequestError.Category.AUTHENTICATION_REOPEN_SESSION)) {
Log.e("Facebook parsing data","The facebook session was invalidated.");
//onLogoutButtonClicked();
} else {
Log.e("Facebook parsing data","Some other error: "+ response.getError().getErrorMessage());
}
}
}
});
request.executeAsync();
I have tried everything but where ever i do i am getting email null :(
After setting the permissions as above access the email via: user.asMap().get("email"));
You should get the email like this
String email = (String) response.getGraphObject().getProperty("email");
if(email!=null && !email.equals("")
//email variable should've email id.
Try removing the permission list in the login call.
ParseFacebookUtils.logIn(SignUpActivity.this, new LogInCallback() {
//you can add permissions on later screens.
}
Please add you this request:
Request request = Request.newMeRequest(ParseFacebookUtils.getSession(),new Request.GraphUserCallback()
In login request condition => user.isnew()
else if (user.isNew()) {
Log.w("FB","User signed up and logged in through Facebook!");
//showUserDetailsActivity();
getProfileInfo();
}
write your block in between this and you will be able to get new user info with email also.
This perfectly works in iOS and sure will help in android.
Before the line
request.executeAsync();
add these three lines.
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
Now you will get email in your response
I am working on an application that uses Parse and I want to login using Facebook.
This is my code:
Button facebook = (Button)findViewById(R.id.facebook);
facebook.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
ParseFacebookUtils.logIn(signUpStep2.this, new LogInCallback() {
public void done(ParseUser user, ParseException err){
if (user == null) {Log.d("My app", "NULL");}
} else if (user.isNew()){Log.d("My app", "New User");}
else{Log.d("My app", "User");}
However, I am always getting a Null user, and a Null ParseException err.
What is my error?
try to set permission on login method.
you can also check these links: Android Facebook login returns null user and ParseFacebookUtils.LogIn I keep getting a null ParseUser object
ParseFacebookUtils.logIn(Arrays.asList("email",Permissions.Friends.ABOUT_ME,Permissions.Friends.BIRTHDAY),
this, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
Log.d("My app", "NULL");
} else if (user.isNew()) {
Log.d("My app", "New User");
} else {
Log.d("My app", "User");
}
}
});