-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Query in Parse Cloud returns "unauthorized" error #356
Comments
Are you requiring/defining |
No, I am not. The code above is exactly the cloud/main.js file. |
Are you setting any client keys in the ParseServer initialization? (rest, js, client, dotnet)... If so, remove them. |
My settings is according to the Parse Server example:
If I remove the keys ( |
I think I have the same problem as @Leo-One. I can't use any queries in cloud code using the parse server as they return with the error When diving into the source to troubleshoot I saw that all the queries (find, first, get etc) results in I'm probably missing something here, but what would you have to do to get queries working in cloud code with a local parse server? |
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'
})); |
@simonbengtsson Just out of curiosity, what is the hosting solution are you using ? I am using Heroku and I tried:
None of above ones worked. |
I've written the command line: Parse.Cloud.beforeSave("Message", function(request, response) {
console.log('Parse.serverURL: ' + Parse.serverURL);
if (!request.object.get("uniqueCode")) {
//more code and it printed What more could be wrong ? |
Im using the parse server locally. The serverurl for heroku would be something like http://your-heroku-app-id.herokuapp.com/parse. Or simply where the parse server is located. |
Along with setting the serverURL in index.js make sure you are also passing a valid sessionToken otherwise it will throw an unauthorized error. |
Also getting a similar response (141 / unauthorised) via Cloud Code. App ID matches client-side and in the client side JS:
=> cloud/main.js:
(nothing declared outside of => |
What is your initialization code for the parse server @jamiechapman? Make sure you are using the serverURL option there as well. |
The
(XXXXXXX's subbed in place of real values, both match server/client side) |
@simonbengtsson Think I sussed it! The environment variable |
In my case, I have no clues about what's happening yet. I do not know how to debug it. There is no additional information. My solution, at moment, is an instance of a custom Express app (connected to the same database of the Parse Server) that handles manually the requests that would have been handled by the triggers. |
Can you share the, preferably simplified, code you are using @Leo-One? What do you mean with custom express app? Can you use queries in the same express app used by the parse server? |
@simonbengtsson the code is simple, almost equals to Parser Server example in github page. index.js var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var databaseUri = process.env.DATABASE_URI || process.env.MONGOLAB_URI;
if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
var app = express();
var port = process.env.PORT || 1337;
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY || '',
clientKey: process.env.CLIENT_KEY || '',
serverURL: 'http://localhost:' + port + '/parse'
});
// Serve the Parse API on the /parse URL prefix
app.use('/parse', api);
app.get('/', function(req, res) {
res.status(200).send('Express is running here.');
});
app.listen(port, function() {
console.log('parse-server-example running on port ' + port + '.');
}); cloud/main.js Parse.Cloud.define('hello', function(req, res) {
res.success('Hi'); // it works
});
Parse.Cloud.beforeSave("Message", function(request, response) {
// it prints http://localhost:29257/parse as expected
console.log('Parse.serverURL: ' + Parse.serverURL);
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);
}
});
}); In iOS Swift app let message = PFObject(className:"Message")
message["content"] = "Hello World"
message["toId"] = userId
message["fromId"] = PFUser.currentUser()!.objectId!
message["uniqueCode"] = "ITSREALLYUNIQUE"
message.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
if let error = error {
print(error.debugDescription)
// it prints Response Body : {"code":141,"error":"Error: undefined unauthorized"}
}
print("success: \(success)")
} I think the following issue is similar. I will check it later: #407 |
@simonbengtsson Yes. You can create another Node.js/Express server and point it to the same database that Parse Server uses. Then you can execute your custom queries and operations. |
I tried your code and for me it works, I used the rest api though. Does it work if you use curl instead? Do your ios app have the correct serverURL?
|
@simonbengtsson Thanks, but I execute the same curl command that you posted and I received the same error This is the code: let config = ParseClientConfiguration(block: {
(ParseMutableClientConfiguration) -> Void in
ParseMutableClientConfiguration.applicationId = "APP_ID";
ParseMutableClientConfiguration.clientKey = "CLIENT_KEY";
ParseMutableClientConfiguration.server = "https://nameofapp.herokuapp.com/parse";
});
Parse.initializeWithConfiguration(config); I noted that all tests that were posted here were in localhost. Maybe can it be a Heroku problem ? |
I've been facing the unauthorized issue with every save from ParseCloud. Using @simonbengtsson tip to include serverUrl during app initialization worked for me. |
@Leo-One Definitely worth a shot trying locally! If that doesn't work, could you post the status code of the http request? |
Hi, we are having this same issue. In our case, using either object.fetch() or query.get(object.id) is failing on parse-server v2.1.2 with an error: Work around is to explicitly set the Parse.serverURL within the Cloud code. cloud/main.js
|
are you passing the serverURL property to the ParseServer initialization? That's pretty important now.. |
@simonbengtsson I am trying it locally and I created an Message via REST Api. I noticed that the error occurs here: // middleware.js, line 90, function handleParseHeaders(req, res, next)
// Client keys are not required in parse-server, but if any have been configured in the server, validate them
// to preserve original behaviour
var keyRequired = req.config.clientKey || req.config.javascriptKey || req.config.dotNetKey || req.config.restAPIKey;
var keyHandled = false;
if (keyRequired && (info.clientKey && req.config.clientKey && info.clientKey === req.config.clientKey || info.javascriptKey && req.config.javascriptKey && info.javascriptKey === req.config.javascriptKey || info.dotNetKey && req.config.dotNetKey && info.dotNetKey === req.config.dotNetKey || info.restAPIKey && req.config.restAPIKey && info.restAPIKey === req.config.restAPIKey)) {
keyHandled = true;
}
console.log("keyRequired: " + keyRequired);
console.log("keyHandled: " + keyHandled);
if (keyRequired && !keyHandled) {
return invalidRequest(req, res);
} Then tried to include X-Parse-Client-Key in the request header (same value that is passed to server in index.js) and it does not work. The problem is to set the client_key. Even the swift SDK is not sending client key (please see below), neither the client key in REST API request header has worked. Is this the expected behaviour ? Headers : ["X-Parse-Installation-Id": "4fop473b-c46d-4951-bf3w-c945ff027748", "X-Parse-Session-Token": "r:8bd43330d81aa37446b2cf0361222bcf", "Content-Type": "application/json; charset=utf-8"]
Request Body : "{\"content\":\"Hello\",\"userId\":\"ad4bvIlGYW\", ... }" |
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. |
@Leo-One @simonbengtsson check the PR branch, let me know if it still occurs on that branch. |
I was having similar issue, "unauthorized", or query constraints not functioning properly. I had all the latest versions of the SDK's and parse-server - OR SO I THOUGHT. I had run all the commands to update the server and it said parse-server@2.1.2. Then I started learning a little about node and ran some check commands like npm list and low and behold, some how in the list was parse-server@2.0.0. I uninstalled parse-server anywhere I could find it (may have been more then one) re installed parse-server ran npm list it showed 2.1.2 and just like that everything worked. No more unauthorized, queries worked etc. I don't have any client key (JS, rest, etc) set. In the PHP initializer for the restKey I just set it to "notUsed" because you need 3 and it works like a charm. Not sure if this will help but I can begin to tell you how happy it made me for this to work. |
@flovilmart Nope that fixes it! At least for the small test case I used (similar to what you tested I guess). |
Awesome! |
let us know if it reappears |
Hello Friends, I'm also getting similar "unauthorized" error. I've deployed parse on heroku. I'm successfully able to call the hello cloud function but when try to query on database I'm getting following errors.
This is my code :
Any help/assistance will be highly appreciated. Thank you in advance. |
Are you setting up |
Hi @simonbengtsson, Thank you for your reply.
Do you need any other information to provide your assistance ? Thank you very much again !! |
add the serverUrl option to the ParseServer initialization. |
Hi @gfosco @simonbengtsson I've added serverUrl option l in ParseServer initialization.
When I execute following simple cloud function that save document in collection, I'm getting same "unauthorized" error.
I'm calling "addToGameScore" function from Postman/Curl. I think still I'm missing some configuration. Thank you for continous assistance. |
Hi All, I have created new parse app on heroku and issue resolved !!!! Thank you all for your assistance and guidance. |
HELLO GUY . I NEED HELP PLEASE |
// Enable Local Datastore.
|
PLEASE LET ME KNOW HOW I CAN FIX THIS BECAUSE I SPEND 4 DAYS IN IT . I CANT FIX IT |
You're explicitly setting the client key to a null value in that code.. On Thursday, March 24, 2016, eustache509 notifications@github.com wrote:
|
Thanks Fosco but it save still fail |
03-24 21:23:40.048 4014-4628/? W/System: Ignoring header X-Parse-Client-Key because its value was null. |
@OverRide
|
03-25 00:44:10.737 1300-1300/? W/PackageParser: Ignoring duplicate uses-permissions/uses-permissions-sdk-m: android.permission.INSTALL_GRANT_RUNTIME_PERMISSIONS in package: com.android.shell at: Binary XML file line #101 |
it give me different message now but , it still save fail |
I was having the same issue and initially i though it was the cloud code that needed update. After a lot of troubleshooting I realised that the code was fine. Then I tried to see whether parse server was setup correctly. I am running it on IBM BlueMix (should be the same as heroku). The problem was the following
The server url was resolving to "http0.0.0.0:61026/parse", which was incorrect. I updated it to include :// and the problem was resolved. The new configuration looked like the following:
Such a simple mistake took me 4 hours to resolve. |
var express = require('express'); var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI; if (!databaseUri) { var api = new ParseServer({ Parse.Cloud.define("articles", function(request, response) { |
@wellkeptbeauty can you open a new issue with detailed informations? Your issue is most likely an implementation issue and not a Parse-server issue. |
@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
|
any solution please |
Did you check @dplewis response? |
The command
query.find
always returns error in Parse Cloud. The response isResponse 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 ?
The text was updated successfully, but these errors were encountered: