com.parse.ParseRequest$ParseRequestException: invalid session token - android

Hello I am working on android app in which I am using parse cloud. I have signUp into the system then after I am trying to fetch data from parse.
But I am getting an exception everytime
com.parse.ParseRequest$ParseRequestException: invalid session token
String userName = ParseUser.getCurrentUser().getUsername();
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("users");
parseQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException parseException) {
}
});
How we can resolve this problem.

Googling and Parse docs didn't give too much info about this exception, but There are few common mistakes I found. You should treat users as ParseUser, not ParseObject.
ParseQuery<ParseUser> query = ParseUser.getQuery();
One more case: need to specify what to find in background. If it is username, so write:
parseQuery.whereEqualTo("username", userName);
And finally callback will contain List with ParseUsers, not ParseObjects
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
}
});
I'm not sure exception will be gone, but I hope this answer will be useful anyways.
Some useful links: doc with example, answer, Doc for class ParseQuery with examples
UPDATE
This is the official doc how to handle this error, also as I commented try to use ParseUser.enableRevocableSessionInBackground() after Parse.initialize(); According to the SDK Documentation it is gonna update session token and only one case it could be invalid - ParseObject was removed.
Hope that helps.

I got this error when I was trying to login while I already logged in.
Try to call ParseUser.logout()

As Yurets said, you probably removed the session object. This can be solved quickly by uninstalling the app and then re-installing it.

Related

Get all users by Role Parse Android

Hi I have the next problem:
I have 10 users registered in Parse,a user can have the role of Admin or Player.
I'm trying to get all the users first but when I tried, the query only return one (The current user).
How can I get all the users? And then how can I filter by Role?
I have the next code:
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
if (e==null){
Log.i("OK","no problem");
}else{
Log.i("NOT OK","problem");
}
}
});
Honestly, for something as simple as this, it would be better for you not to use relations and separate classes at all and instead to simply add a string field to the User Class called "role." So go to your User class on Parse, add a new column of type String called "role" and then query it as follows.
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.whereEqualTo("role", "admin");
userQuery.findInBackground(new FindCallback<ParseUser>() {
void done(List<ParseUser> results, ParseException e) {
// results has the list of users who are admins
}
});
You should check the ACL(readAccess) of each user. The user is without read access right. The other 9 user may not with public read, not specify the user's id, and the role is not equip access right.
As A.Vin's answer, if your user have more than 1 role, you can use array of strings.

Check for username's existence | Parse.com

I'm searching for a good way of querying usernames towards my app's user-database. So that people can't signup with the same username as someone else.
I'm using a username, password and an email for recover of password. So I want to check for username's basically.
I've understood this can be achieved with ParseQuery?
I would like an example if anyone can provide?
You can use a ParseQuery to query all usernames in the database like this.
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
// objects is a list of all the User you have. Just get all the usernames,
//and check if there's already one equal to the one you want to insert.
} else {
//error handling
}
}
}

How to fix "Unchecked call to findInBackground" with Parse.com API

I'm going through an Android app I've ported from iOS and trying to figure out the causes of any warnings I'm getting and get my code in line with Android Studio's lint.
So far, most of the "problems" have been pretty easy fixes, but I've got a warning with a particular Parse.com API function that has me stumped.
Here's the code:
private void loadData() {
ParseQuery query = ParseQuery.getQuery("someClass");
query.include("someProperty1");
query.include("someProperty2");
query.include("someProperty3");
query.orderByAscending("sequence");
query.whereEqualTo("owner", ParseUser.getCurrentUser());
// This next line causes the warning
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> items, ParseException e) {
if (e != null) {
// Error handling here
} else {
// Got query results here
}
}
});
}
The warning that is showing up is:
Unchecked call to 'findInBackground(FindCallback < T >)' as a member of raw type 'com.parse.ParseQuery'.
When you allow autocomplete to "fill out" a new FindCallback for you, you get this:
query.findInBackground(new FindCallback() {
#Override
public void done(List list, ParseException e) {
}
#Override
public void done(Object o, Throwable throwable) {
}
});
Well, that's annoying, because the actual results that are returned are a List of type ParseObject. So, I edited that code to my code above in order to streamline my code. The interesting thing is that even the autocomplete code has the same warning associated with it.
I think I understand to reason that this warning is generated, but I've attempted as best I can to specify my types. Is there something more I can do to make my code work? Or rather, given that I can't re-write Parse's code, how do I correctly fix this warning?
You just need to type your ParseQuery:
Replace ParseQuery query with ParseQuery<ParseObject> query

ParseQuery gives 0 objects when querying from local datastore even after successfully pinning

I am using the Android SDK of parse.com and have arrived at a peculiar problem.
From a fragment's onCreate:
Fetch an object from the server.
Pin it.
Fetch an object from the local datastore.
Here is a snippet of the code from the onCreate:
ParseObject myChatGroup = ParseObject.createWithoutData("ChatGroup", "mOJGWRiLPC");
myChatGroup.fetchInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject chatGroup1, ParseException e) {
if (e == null) {
l.d("Fetched chat group: " + chatGroup1 + " from server");
chatGroup1.pinInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
l.d("Successfully pinned chat group");
ParseQuery<ParseObject> chatGroupParseQuery = new ParseQuery<>("ChatGroup");
chatGroupParseQuery.fromLocalDatastore()
.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
l.d("Found " + list.size() + " chat groups from local datastore");
} else {
e.printStackTrace();
}
}
});
} else {
e.printStackTrace();
}
}
});
} else {
e.printStackTrace();
}
}
});
Here is the log:
Fetched chat group: ChatGroup { objectId: mOJGWRiLPC, name: Admin } from server
Successfully pinned chat group
Found 0 chat groups from local datastore
But this doesn't make any sense! I just pinned an object so how can there be 0 objects in the local datastore. The code is so simple. What am I doing wrong? Could it be a bug with the SDK?
Any insight will be much appreciated I have been trying to find the issue for days now.
The Parse setup looks fine to me:
ParseObject.registerSubclass(ChatGroup.class);
Parse.enableLocalDatastore(this);
Parse.initialize(this, AppProps.properties.appId,
AppProps.properties.clientKey);
ParseUser.enableRevocableSessionInBackground();
Parse.setLogLevel(Parse.LOG_LEVEL_DEBUG);
Note:
It works fine when trying the same logic with the ParseUser object. And even other classes of mine like my Message which leads me to suspect that something is wrong with my ChatGroup class.
I have added two fields to my ChatGroup class on the parse.com data browser: name(String) and messages(Relation) with read and wrote access to a role called Admin.
When browsing to the actual parse db (using root access) I find that the database table (ParseObjects) does contain the row that I pinned. But somehow parse shows 0 results on querying it!
Parse doesn't seem to support ACLs on local datastore. So the easiest way to get around this issue is to do ignoreACLs() on the query -- assuming you do have roles/acls setup correctly on the server side, so whatever you have pinned locally should be OK permission-wise already.
I found the issue. It seems to be more like a bug with the Android SDK of parse. I narrowed it down to an issue with retrieving pinned objects with special ACLs using the Android SDK.
Initially, the object had the ACL such that only the role 'Admin' (that I created) can read and write. The funny thing is that the user with which I was testing the pinning and querying was an 'Admin'! But, when I changed the ACL such that public can read but only Admin can write, the problem was resolved!
My theory was that querying the local datastore behaves like a public query, no matter what role the user that is making the query has! But if any other theory explaining this would be appreciated. Luckily, in my case I need public read access, but this bug can be a huge downfall for some who need to have read access restricting ACLs! Hopefully parse.com will fix this issue soon.
This is quite a big issue! I am surprised that I couldn't find a question regarding this issue.
Yeah so basically you have to set ACL for current user and pin it before you pin the chat group.
ParseACL acl = new ParseACL();
acl.setReadAccess(ParseUser.getCurrentUser(), true);
ParseUser.getCurrentUser().setACL(acl);
ParseUser.getCurrentUser().pinInBackground();

Saving data into Parse.com cloud for another user

I have an app which works with users, and offers the possibility to follow certain users, at the current user choice.
Unfortunately, I don't know how to modify data of a user which is not the current user. I have not seen anything like that stated in the Parse.com docs(or i missed).
I have a column in my database in "Users" class called "usersFollowed" and when the current user clicks "Follow" i want to add the current user to the "usersFollowed" list, but adding them like you will see in the following code does not work.
Follow user code:
dialog = ProgressDialog.show(context, "",
"Following...", true);
viewHolder.userFollow.setSelected(true);
viewHolder.userFollow.setText("FOLLOWING");
ParseQuery<ParseUser> userListQuery = ParseUser.getQuery();
userListQuery.whereEqualTo("screenName", parseUserList.get(position).get("screenName").toString());
userListQuery.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> parseObjects, ParseException e) {
parseUserFollowedList = new ArrayList<>();
if (parseObjects.get(0).getList("usersFollowed") == null) {
parseUserFollowedList.add(ParseUser.getCurrentUser());
parseObjects.get(0).addAll("usersFollowed", parseUserFollowedList);
} else {
parseUserFollowedList = parseObjects.get(0).getList("usersFollowed");
parseUserFollowedList.add(ParseUser.getCurrentUser());
parseObjects.get(0).addAll("usersFollowed", parseUserFollowedList);
}
parseObjects.get(0).saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
dialog.dismiss();
}
});
}
});
.saveInBackground does not work because it throws exception: Cannot save info for user that is not logged in".
Can anyone help me in how to do this?
Cheers!
Users may modify only their own data. But the idea of following can be implemented such that only the currentUser's record requires write access: If Jack chooses to follow Jill, then Jack writes to his "following" relation. This works as long as we don't try to represent "followedBy" in Jill's data (which can be achieved instead with a query).
Or consider that the User table represents the private relationship between a real person and your app. It might make better sense to model the idea of a user's public face with your own custom object, and model following relationships between those. I mention this idea elsewhere here and here.

Categories

Resources