Facebook AccessToken is null on re-opening application after a "full" close - android

I have an app that uses Facebook in combination with Parse to login users.
My problem is that when I fully close the application and then open it again, my AccessToken from Facebook is null. Seems like it is not stored from Session to session for some reason.
When I just log out and then in again, it works fine. Anyone experienced the same issue?
This is how I log in the users:
List<String> permissions = Arrays.asList("public_profile");
ParseFacebookUtils.logInWithReadPermissionsInBackground(this, permissions, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
//Error...
} else if (user.isNew()) {
//Some code...
mCurrentUser = ParseUser.getCurrentUser();
} else {
//Some code...
mCurrentUser = ParseUser.getCurrentUser();
}
}
});

add Parse.enableLocalDatastore(this); in onCreate() of your_app extends Application

Related

Unable to Signup as Parseuser, after failed signup call

I am using Parse.com for my android app.
I have enabled ParseAnonymousUser
I am getting the following error, when test with the following scenario
---> Turn off wifi, and try to signup
---> Then Turn wifi on, without killing the app, and then try to signup again.
I get the the error: "java.lang.IllegalArgumentException: Cannot sign up a user that is already signing up." (error code: -1)
---> If i kill the app, then i successfully signup
Here is my signup code snippet:
ParseUser user = ParseUser.getCurrentUser();
user.setUsername(phoneNum);
user.setPassword(phoneNum);
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.show();
user.signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
if (getActivity() == null) {
return;
}
mProgressDialog.dismiss();
handleSignupResponse(e, phoneNum, name);
}
});
Please tell me a solution asap. I have been struck here for a day.
You have a mistake in declaring the ParseUser object.
Your mistake:
ParseUser user = ParseUser.getCurrentUser();
The user is not existed yet, and you are requesting it, that doesn't make any sense.
It should be:
ParseUser user = new ParseUser();
And then your rest of the code.

Facebook asking for login again Android

this is my code which uses Parse to log a user in via Facebook.
public void onFacebookLoginClick(View v) {
ParseUser currentUser = ParseUser.getCurrentUser();
if ((currentUser != null) && ParseFacebookUtils.isLinked(currentUser)) {
Log.d("onFacebookLoginClick", "Already signed in");
showHomeActivity();
} else {
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("Facebook", "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d("onFacebookLoginClick: ", "User signed up and logged in through Facebook!");
showHomeActivity();
} else {
Log.d("onFacebookLoginClick: ", "User logged in through Facebook!");
showHomeActivity();
}
}
});
}
}
The problem is, the user logs in the first time (OK!), then the login dialog opens up again.
Note: When I close the dialog, it does take me to the next activity. But why can it be showing the login dialog twice?
I had a similar issue implementing this. I solved it by letting my code sleep for a few seconds before attempting to start the activity. It's possible this may be the bug.
--Why I arrived at this conclusion--
I found the loginCallBack in ParseFacebookUtils takes a few seconds to return with its result code/parse user.
No data was returned by the time I start the activity and the activity is configured to launch the login activity if no user is logged in. This made it appear as though the code wasn't logging in on first try. I believe the Parse Facebook logic is a little slower than the regular parse login logic.
The second time you run the code (click the button) the callback was already returned with the parse user and would successfully launch the activity without returning.

Using ParseFacebookUtils.logInWithReadPermissionsInBackground does not save Facebook permissions Android

After Facebook Login with ParseFacebookUtils im trying to Log the user's permissions but its always empty, here's my code:
List<String> permissions = Arrays.asList("user_birthday", "user_location", "user_friends", "email", "public_profile");
ParseFacebookUtils.logInWithReadPermissionsInBackground(this, permissions, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if (user == null) {
Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
} else {
updateUser(user.isNew());
}
}
});
Later...
Log.d(Check.TAG, AccessToken.getCurrentAccessToken().getPermissions().toString()); //returns empty!!
i have also tried
AccessToken.getCurrentAccessToken().getPermissions().containsAll(permissions)
and always its empty.
Im using Android Parse Library 1.9.1 and Facebook SKD 4.0.1
This is driving me nuts! please help!!

Android - Facebook session CLOSED when using ParseFacebookUtils.logIn

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.

Link/unlink existing ParseUser to Facebook account in Android

I'm trying to link an existing user to his or her Facebook account using Parse. After logging into through Parse, the user can go to the SettingsActivity and link their Facebook account.
I achieved this by calling ParseUser.logInInBackground and then verified it by checking if ParseUser.getCurrentUser() != null.
In my SettingsActivity, the user can press a 'Connect to Facebook' button, which is supposed to link the account to Facebook, but it's not working. When the user clicks the button, I executed this code below, as per the Parse Android documentation:
mUser = ParseUser.getCurrentUser();
public void onToggleFacebookConnectedClick(View v) {
if (!ParseFacebookUtils.isLinked(mUser)) {
ParseFacebookUtils.link(mUser, this, new SaveCallback() {
#Override
public void done(ParseException ex) {
if (ParseFacebookUtils.isLinked(mUser)) {
Log.d(Application.APPTAG, "Woohoo, user logged in with Facebook!");
}
}
});
} else if(ParseFacebookUtils.isLinked(mUser)) {
ParseFacebookUtils.unlinkInBackground(mUser, new SaveCallback(){
#Override
public void done(ParseException ex) {
if (ex == null) {
Log.d(Application.APPTAG, "The user is no longer associated with their Facebook account.");
}
}
});
}
}
I am getting the error: com.parse.ParseException:java.lang.IllegalArgumentException: Cannot save a ParseUser until it has been signed up. Call SignUp first.
The user has already signed up (not using Facebook), so I am confused as to why I'm am getting this message. How can I resolve this issue?
It turns out I made a silly mistake. I wasn't linking the user correctly because my Facebook Application ID was not set correctly in my Parse App Settings.

Categories

Resources