Skip to content

'unauthorized' error using 2.0.8 #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RadianSmile opened this issue Feb 14, 2016 · 23 comments
Closed

'unauthorized' error using 2.0.8 #407

RadianSmile opened this issue Feb 14, 2016 · 23 comments
Labels
type:question Support or code-level question

Comments

@RadianSmile
Copy link

I run Parse server @2.0.8

then happened a strange thing :

I ran the sample code in node :

...
Parse = require ('parse/node');
var obj = new Parse.Object('GameScore');
obj.set('score',1337);
obj.save().then(function(obj) {
  console.log(obj.toJSON());
  var query = new Parse.Query('GameScore');
  query.get(obj.id).then(function(objAgain) {
    console.log(objAgain.toJSON());
  }, function(err) {console.log(err); });
}, function(err) { console.log(err); });
...

then my server log showed :

2016-02-14T11:54:01.166014+00:00 app[web.1]: { score: 1337,
2016-02-14T11:54:01.166021+00:00 app[web.1]:   createdAt: '2016-02-14T11:54:00.537Z',
2016-02-14T11:54:01.166022+00:00 app[web.1]:   updatedAt: '2016-02-14T11:54:00.537Z',
2016-02-14T11:54:01.166023+00:00 app[web.1]:   objectId: 'rzuNqFVFY0' }
2016-02-14T11:54:01.201652+00:00 app[web.1]: ParseError { code: undefined, message: 'unauthorized' }

that means it could create object , but not able to query.

that's so wired. So I test it with
parse-server@2.0.0

then this code runs smoothly :

2016-02-14T12:14:01.634098+00:00 app[web.1]: { score: 1337,
2016-02-14T12:14:01.634103+00:00 app[web.1]:   createdAt: '2016-02-14T12:14:00.587Z',
2016-02-14T12:14:01.634104+00:00 app[web.1]:   updatedAt: '2016-02-14T12:14:00.587Z',
2016-02-14T12:14:01.634106+00:00 app[web.1]:   objectId: '2MbFbROSAP' }
2016-02-14T12:14:01.685361+00:00 app[web.1]: { score: 1337,
2016-02-14T12:14:01.685366+00:00 app[web.1]:   updatedAt: '2016-02-14T12:14:00.587Z',
2016-02-14T12:14:01.685367+00:00 app[web.1]:   createdAt: '2016-02-14T12:14:00.587Z',
2016-02-14T12:14:01.685367+00:00 app[web.1]:   objectId: '2MbFbROSAP' }

Please check! Thanks.

@RadianSmile RadianSmile changed the title version 2.0.8 version 2.0.8 has problem Feb 14, 2016
@henry40408
Copy link

what's version of your mongo server?

@RadianSmile
Copy link
Author

I use heroku mongolab addon with sandbox plan : http://docs.mongolab.com/ops/#version-mgmt
version is 3.0.x

@davide-scalzo
Copy link

Have you tried initialising Parse with the JS key?

@jmk2142
Copy link

jmk2142 commented Feb 14, 2016

Hm. You shouldn't have to use a JS key, as long as you don't create your parse server instance with one as option. Check your DB and look at the documents in your Schema collection. Is there anything strange under metadata.find property?

@gfosco
Copy link
Contributor

gfosco commented Feb 14, 2016

Where is this code running (cloud code or a separate node script) ? Was Parse initialized, was the serverURL set? If it's in cloud code, you should remove the require on Parse, as it is already a global.

@polo2244
Copy link

I have the same problem. And it is solved (at least for me) by including { useMasterKey: true } in each query.
like this :
var query = new Parse.Query(Parse.User); query.equalTo('objectId', request.user.id); query.first({ useMasterKey: true }).then(function(user) { .....

This is indicated here https://github.com/ParsePlatform/parse-server in the last paragraph under *Not Supported *

Parse.User.current() or Parse.Cloud.useMasterKey() in cloud code. Instead of Parse.User.current() use request.user and instead of Parse.Cloud.useMasterKey() pass useMasterKey: true to each query. To make queries and writes as a specific user within Cloud Code, you need the user's session token, which is available in request.user.getSessionToken().

But as far as i know this is not a solution at all. Since some Queries do not require the use of MasterKey.

Also, i was not able to Fetch the user even with a master key. The only way to retrieve the User was to query Parse.User with an ObjectId constraint.
This did not work for me currentUser.fetch({ useMasterKey: true }).then(function(currentUser) { ...

@gfosco gfosco changed the title version 2.0.8 has problem 'unauthorized' error using 2.0.8 Feb 14, 2016
@RadianSmile
Copy link
Author

@davodesign84 no js key
@jmk2142 I use mongolab.com, sorry that I cant get what you said. where to find metadata.find property
@gfosco please see the following details :

my Parse server config :

//# app.js 

// express@4.13.4

ParseServer = require('parse-server').ParseServer;
app.use(PARSE_MOUNT, new ParseServer({  // mount on '/1'
  databaseURI: DATABASE_URI,
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: 'id',
  masterKey: 'mkey'
}));

I ran these in node server , not parse cloud.

//#  app.js 

// this is my initialization
Parse = require ('parse/node');
Parse.initialize("id","");
Parse.serverURL = PARSE_SERVER_URL; 

// do request and then get unauthorized when query.
Parse = require ('parse/node'); // parse js sdk @1.6.14
var obj = new Parse.Object('GameScore');
obj.set('score',1337);
obj.save().then(function(obj) {
  console.log(obj.toJSON());
  var query = new Parse.Query('GameScore');
  query.get(obj.id).then(function(objAgain) {
    console.log(objAgain.toJSON());
  }, function(err) {console.log(err); });
}, function(err) { console.log(err); });

Another thing. I don't know is this related.

PARSE_SERVER_URL in the parse server example repo is
https://xxx.herokuapp.com/ it works for me before(not sure). but I now would see this wrong result :

2016-02-14T12:04:48.823496+00:00 heroku[router]: at=info method=POST path="/classes/GameScore" host=nccu-ctld.herokuapp.com request_id=5c2626cd-143c-4082-b3de-c43cb4866a28 fwd="174.129.148.88" dyno=web.1 connect=2ms service=316ms status=404 bytes=1670
2016-02-14T12:04:48.827611+00:00 app[web.1]: POST /classes/GameScore 404 307.577 ms - 1433
2016-02-14T12:04:48.834256+00:00 app[web.1]: ParseError {
2016-02-14T12:04:48.834258+00:00 app[web.1]:   code: 107,
2016-02-14T12:04:48.834259+00:00 app[web.1]:   message: 'Received an error with invalid JSON from Parse: <!DOCTYPE html><html><head><title></title><link rel="stylesheet ... / whatever, it's 404 page.

The request went to '/' to do requests.
Then I set parse_server_url to https://xxx.herokuapp.com/1 and get the right result.

@RadianSmile
Copy link
Author

the following info maybe useful but strange too :

2016-02-14T11:54:01.160595+00:00 heroku[router]: at=info method=POST path="/1/classes/GameScore" host=xxx.herokuapp.com request_id=cc699c78-c645-43b8-b187-68d0d3717ff1 fwd="xx.x.x.xx" dyno=web.1 connect=1ms service=657ms status=201 bytes=468
2016-02-14T11:54:01.166014+00:00 app[web.1]: { score: 1337,
2016-02-14T11:54:01.166021+00:00 app[web.1]:   createdAt: '2016-02-14T11:54:00.537Z',
2016-02-14T11:54:01.166022+00:00 app[web.1]:   updatedAt: '2016-02-14T11:54:00.537Z',
2016-02-14T11:54:01.166023+00:00 app[web.1]:   objectId: 'rzuNqFVFY0' }
2016-02-14T11:54:01.201652+00:00 app[web.1]: ParseError { code: undefined, message: 'unauthorized' }

there should be two router log showing two request reaches/1/classes/GameScore , but there is only one router log (I have double checked). So it means ParseError happend before the request reach /1/classes/GameScore?! the problem seems is in request side (parse-js-sdk) but not server-side (parse-server). but my problem solved when server go back to 2.0.0 and Parse-js-sdk is always1.6.14.... why?

@polo2244
Copy link

@RadianSmile i have a question about using parse server version Parse server @2.0.8
I installed parse-server-example on Heroku using the Deploy button at (https://github.com/ParsePlatform/parse-server-example) and resulted with the first build 1.0.0

How did you update to the latest build 2.0.8 ?

It was pointed out to me to change the version here:
`{ "name": "parse-server-example", "version": "1.0.0",.... ' inside package.jon to 2.0.8 and it should work. But i don't think this is the way to do it. Is there another way ?

@RadianSmile
Copy link
Author

@polo2244 It seems that you mistaken project version as module version. You should look at "dependencies" property in package.json. There goes "parse-server": "~2.0".

You should read the package.json as:

Your projcet "parse-server-example" is now 1.0.0 version (the version can be defined by yourself) and depend on parse-server Approximately at version 2.0

@jmk2142
Copy link

jmk2142 commented Feb 16, 2016

@RadianSmile there should be a "table/collection" in MongoLab's data browser called schema. You'll find objects representing your other classes you produced. Basically you can set class permissions and such there including query permissions. Dig around a little and make sure you don't have anything weird in there. If you have a new app, with all class permissions open it'll basically be a bunch of empty objects. From the rest of this thread it seems this is probably not the culprit but check just to be sure. If something is funny there it will probably pop out at you.

@simonbengtsson
Copy link
Contributor

@RadianSmile What serverURL are you using in the parse cloud (for the parse server)? You didn't include any in the config above, but if you are using heroku it should be something like https://heroku-app-id.herokuapp.com/parse.

@eatentree
Copy link

Make sure you use latest version of parse sdk.

@mahabubakram
Copy link

I am performing this but its not working

Parse.Cloud.afterSave("MyClass", function (request) {
    console.log("AFTER SAVE EXECUTED");
    var myToken = request.object;
    console.log(myToken)
    myToken.set("house", 852);
    myToken.save({
        useMasterKey: true,
        success: function(res){
            console.log(res)
        },
        error: function(err){
            console.log(err);
        }
    });
});

It prints the console log but it fails to change the object which have been triggered this afterSave().

Help needed ASAP.

@simonbengtsson
Copy link
Contributor

You should not save the same object as triggered the aftersave as that will create an infinite loop. You main problem is that the firat parameter in the save method is attributes, not options. Use myToken.save(null, {__options__});

@mahabubakram
Copy link

"null" added but still not working. Its giving this error:

it gives error status=201

@simonbengtsson
Copy link
Contributor

I haven't tried but I would imagine that saving the same object that triggered the afterSave (as you do above) would create an infinite loop. If you need to add something to an object, do it in beforeSave instead.

@mahabubakram
Copy link

But I used to do change the same object in the afterSave in my previous parse cloud. But right now the parse server is running in heroku and its creating this problem.

@simonbengtsson
Copy link
Contributor

@RadianSmile Do you still get the unauthorized error with version 2.1.3?

@gfosco
Copy link
Contributor

gfosco commented Feb 26, 2016

@mahabubakram If you're having an issue with the latest version, please open your own issue and provide as much detail as possible, including code. Thanks.

@gfosco gfosco closed this as completed Feb 26, 2016
@RadianSmile
Copy link
Author

@simonbengtsson I am currently not able to test it. I would try latest version recently and tell if any problems. Thanks!

@mindgrub-jkliphouse
Copy link

mindgrub-jkliphouse commented Apr 18, 2016

Hi all, I'm having the same issue. I'm set up and this curl returns all my users:

curl -X GET \ -H "X-Parse-Application-Id: myAppId" \ -H "Content-Type: application/json" \ http://localhost:1337/parse/classes/_User

But this query gets {"message":"unauthorized"}:


var query = new Parse.Query(User);
query.useMasterKey = true; //tried it like this or by passing an object into find
query.find().then(function(result) {
    if(result != null) console.log(JSON.stringify(result));
    else renderInvalidLogin(res);
},
function(error) {
    console.log(JSON.stringify(error));
    renderInvalidLogin(res);
});```

@arkangelx
Copy link

I keep getting this
at=info method=POST path="/parse/1/users" host=xxx-xxx-60991.herokuapp.com request_id=60419993-4a9f-4ce0-9c5c-9d66b2baacb6 fwd="xx.xx.5xx.xxx,91.208.239.222" dyno=web.1 connect=1ms service=8ms status=404 bytes=542

@mtrezza mtrezza added type:question Support or code-level question and removed 🔧 troubleshooting labels Jul 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question Support or code-level question
Projects
None yet
Development

No branches or pull requests