parse-server: Query in Parse Cloud returns "unauthorized" error

The command query.find always returns error in Parse Cloud. The response is Response Body : {"code":141,"error":"Error: undefined unauthorized"}. The request is being done by Swift SDK. It looks that Parse Cloud can’t access the classes of Parse Server.

What could be possible reasons ?

Parse.Cloud.beforeSave("Message", function(request, response) {
    if (!request.object.get("uniqueCode")) {
        response.error('A Message must have a uniqueCode.');
    } else {
        var Message = Parse.Object.extend("Message");
        var query = new Parse.Query(Message);
        query.equalTo("uniqueCode", request.object.get("uniqueCode"));
        query.find({
            success: function(results) {
                if (results.length > 0) {
                    response.success();
                } else {
                    response.error("A Message with this uniqueCode already exists.");
                }
            },
            error: function(error) {
                response.error("Error: " + error.code + " " + error.message);
            }
        });
    }
});

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 67 (31 by maintainers)

Most upvoted comments

Are you setting any client keys in the ParseServer initialization? (rest, js, client, dotnet)… If so, remove them.

What I missed was the serverURL option. Is it the same for you @leo-one? For me the following works perfectly for local development:

app.use('/parse', new ParseServer({
    databaseURI: process.env.MONGOLAB_URI || 'mongodb://localhost:27017/myApp',
    cloud: __dirname + '/cloud/main.js',
    appId: 'myAppId',
    masterKey: 'tmp_master_key',
    serverURL: 'http://localhost:1337/parse'
}));

@flovilmart I ran into this issue today for some reason.

I used the parse-server-example fresh out of the box added restAPIKey: ‘rest’ and got error unauthorized using cloud code.

You can fix this by adding javascriptKey: ‘unused’ or pass in clientKey, dotNetKey, restAPIKey to be undefined.

The issue lies with this line

https://github.com/parse-community/parse-server/blob/master/src/middlewares.js#L19

var info = {
    appId: req.get('X-Parse-Application-Id'),
    sessionToken: req.get('X-Parse-Session-Token'),
    masterKey: req.get('X-Parse-Master-Key'),
    installationId: req.get('X-Parse-Installation-Id'),
    clientKey: req.get('X-Parse-Client-Key'),
    javascriptKey: req.get('X-Parse-Javascript-Key'),
    dotNetKey: req.get('X-Parse-Windows-Key'),
    restAPIKey: req.get('X-Parse-REST-API-Key'),
    clientVersion: req.get('X-Parse-Client-Version')
  };

req.get() is returning undefined but info has some defaults

{ appId: 'myAppId',
  sessionToken: undefined,
  masterKey: undefined,
  installationId: 'fbfa62c3-d753-2059-53b2-b6f1e2578b94',
  clientKey: undefined,
  javascriptKey: 'unused', // if your javascriptKey is 'unused' cloud code will always run
  dotNetKey: undefined,
  restAPIKey: undefined,
  clientVersion: 'js1.9.2',
  clientSDK: { sdk: 'js', version: '1.9.2' }

PLEASE LET ME KNOW HOW I CAN FIX THIS BECAUSE I SPEND 4 DAYS IN IT . I CANT FIX IT

Just thought of something, try to init your parse server with a javascriptKey, cloud code needs it to ‘authenticate’ with the parse server, when the request is sent, if the javascriptKey is not provided, that could be problematic.

Also, does the query work in your case with {useMasterKey: true}?

Just confirming: after removing client_key from Parser Server configuration, everything has worked well (including the triggers). I tested from REST API and Swift SDK.