Followed by Parse doc, I created an anonymous user while app is first launched.
...
Parse.initialize(this, APPLICATION_ID, CLIENT_KEY);
ParseUser.enableRevocableSessionInBackground();
if (ParseUser.getCurrentUser() == null) {
ParseUser.enableAutomaticUser();
ParseUser.getCurrentUser().increment("RunCount");
ParseUser.getCurrentUser().saveInBackground();
}
In the parse core, it shows that a new "User" and "Session" row is successfully added.
...
ParseUser.getCurrentUser().setUsername("Leo");
ParseUser.getCurrentUser().setPassword("sasami");
ParseUser.getCurrentUser().signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
...
}
}
...
Then after I calling the "signUpInBackground" method, I found the just-added User row has been updated, but the just-added Session row was disappear. And consequently the user-related query and execution always returns an
com.parse.ParseRequest$ParseRequestException: invalid session token
P.S.
It doesn't matter if signup successfully or returned error. The Session row disappears anyway.
Related
I am not sure if I am missing a step in the process of linking a Facebook account to an existing Parse User.
This is the code I am using, as per Parse.com
if (!ParseFacebookUtils.isLinked(currentUser)) {
ParseFacebookUtils.linkWithReadPermissionsInBackground(currentUser, getActivity(), null, new SaveCallback() {
#Override
public void done(ParseException ex) {
if (ex == null) {
if (ParseFacebookUtils.isLinked(currentUser)) {
Log.d("MyApp", "Woohoo, user logged in with Facebook!");
}
} else {
Log.e(TAG, ex.getMessage());
}
}
});
}
I am not receiving any type of error, and it will successfully open up the Facebook activity to accept/cancel giving access to my application. The issue that I am finding, is that the authData section inside my User record, inside Parse, is never populated.
What am I doing wrong that my Parse User is not receiving any authData?
Do you happen to add the code to fragment?
If you do that, you should consider about onActivityForResult.
As you know, onActivityForResult is not called normally.
When attempting to login and signup through Facebook, the method isNew() somehow always returns false, even if the "_User" table is empty or if the user is logging in for the first time (signing up).
According to the docs:
isNew(): Indicates whether this ParseUser was created during this session through a call to ParseUser.signUp() or by logging in with a linked service such as Facebook.
Here's the code:
private void loginWithFacebook() {
ParseFacebookUtils.logInWithReadPermissionsInBackground((Activity) getContext(), null, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if (user != null) {
if (user.isNew()) {
onLoginListener.onSignupWithFacebook();
} else {
onLoginListener.onLoginWithFacebook();
}
} else if (e != null) {
handleException(e);
}
}
});
}
Other people have come across similar problems and fixed it, but the solutions they've come up with don't apply to my case:
Facebook login with Parse always returns false in user.isNew() Android
Facebook Login not working properly (Parse)
User.isNew() returns false when it should return true
Apart from that, everything else seems to be working fine. The user data is created and stored on the table just like it normally should. Any suggestions?
remove enableAutomaticUser() if you have written in Application or BaseActivity class
docs says that this method will Enables automatic creation of anonymous users.
remove except this three and try may help you.!!
FacebookSdk.sdkInitialize(getApplicationContext());
Parse.initialize(this,getString(R.string.parse_app_id),getString(R.string.parse_client_key));
ParseFacebookUtils.initialize(this);
In my code I convert an anonymous user to a ParseUser, and the data browser shows the converted user with an undefined authData, as it should.
When the app is closed and re-opened, the if statement to check if an anonymous user is linked returns true instead of false, but the authData is still undefined.
We then call a cloud code function to modify the user's friends, and at this point, the current user authData becomes "Anonymous" again, but the username remains the same.
//Returns true no matter what
if (ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) {
mUsernameText.setText("Not Logged In");
Log.d("testuser", "tes1");
} else {
mUsernameText.setText(ParseUser.getCurrentUser().getUsername());
}
ParseCloud.callFunctionInBackground
("addFriend", params, new FunctionCallback<String>() {
#Override
public void done(String s, ParseException e) {
if (e == null) {
//ParseUser.getCurrentUser()
// .increment(NUM_FRIENDS_KEY);
//When we try to save the user here it becomes anonymous
}
}
});
I'm using parse.com Android sdk, in the parseUser I have added a column pointer to other data class named pointer. I access this data by:
ParseUser user=ParseUser.getCurrentUser();
ParseObject pointer= user.getParseObject("pointer");
pointer.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
}
}
But I have a problem when I try to logout before doing this fetch. I logout by:
ParseUser.logOut();
and when this function is invoked the the app get locked.
You simply need to wrap this call to fetch in a check to see if there is still a logged in user:
ParseUser user = ParseUser.getCurrentUser();
if (user != null) {
// rest of your code here, as we have a currently logged in user
}
I am using Facebook and Parse SDK in my Android app. I followed this tutorial for Facebook login and authentication, changing between fragments for login fragment and main menu fragment depending on whether the session state is OPENED in the Session.StatusCallback. And the app works perfectly before integrating with Parse.
And now I encounter a problem. In the onResume() method of the main menu fragment, I added the following code.
final Session session = Session.getActiveSession();
if(session != null && session.isOpened()) {
Request meRequest = Request.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser graphUser, Response response) {
if(session == Session.getActiveSession()) {
// Check if the session is same as usual
ParseFacebookUtils.logIn(graphUser.getId(), session.getAccessToken(),
session.getExpirationDate(), new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
// The user wasn't saved.
System.out.println("User was not saved to Parse. ");
} else {
// The user has been saved to Parse.
System.out.println("User has successfully been saved to Parse.");
if (user.isNew()) {
// This user was created during this session with Facebook Login.
System.out.println("ParseUser created.");
} else {
// This user existed before.
System.out.println("User exists in Parse. Pull their values: " + user);
}
}
}
});
}
}
});
meRequest.executeAsync();
}
So when the fragment is resumed and the Facebook session is opened, the Facebook user is added to the Parse database so that I can use ParseUser to put and get data afterwards. But the problem happens when using ParseFacebookUtils.logIn(), that it makes the Facebook session CLOSED and invokes the Session.StatusCallback, thus switching the visible fragment back to the login fragment.
I was dealing with this problem all day but cannot find a solution. I have added the code below in an Application but still not work.
Parse.enableLocalDatastore(this);
Parse.initialize(this, Application_id, Client_key);
ParseFacebookUtils.initialize(getString(R.string.facebook_app_id));
Is there a way to fix this? I have read this but no quite good solution is provided. Thanks in advance!
I was trying to do the same thing today. I was using the facebook sdk's UiLifecycleHelper to do the facebook login, make the graph api request and add the user to Parse db. That is how it is done in the example you mentioned - which I think is a bit outdated.
ParseFacebookUtils handles the session and all we need to do is call the login method on it, make the graph api call, get user data like email and update the user field.
Here's some code:
private void parseFacebookLogin(){
ParseFacebookUtils.logIn(this, PARSE_FB_LOGIN_CODE, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
Log.d(tag, "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d(tag, "User signed up and logged in through Facebook!");
Request.newMeRequest(ParseFacebookUtils.getSession(), graphUserCallback).executeAsync();
} else {
Log.d(tag, "User logged in through Facebook!");
}
}
});
}
GraphUserCallback graphUserCallback = new GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null){
getUserDataFacebook(user);
Log.d(tag, response.toString());
}
}
};
private void getUserDataFacebook(GraphUser user){
//get user data here
}
Check out Parse's docs on Facebook login.
Let me know if this works for you.