Skip to content
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

Cant get data via JS or PHP from Parse Server #352

Closed
gateway opened this issue Feb 11, 2016 · 4 comments
Closed

Cant get data via JS or PHP from Parse Server #352

gateway opened this issue Feb 11, 2016 · 4 comments
Labels
type:question Support or code-level question

Comments

@gateway
Copy link

gateway commented Feb 11, 2016

So, im a bit frustrated right now due to the fact that I'm trying to get data out of the parse server via either PHP or JS and I wanted to make sure I wasn't doing something wrong or missing something.

First Im running parse server on Heroku with MongoLab and I have confirmed that its working via CURL command to pull the same data I'm trying to with JS or PHP.

Here is my CURL command that returns results I'm looking for.

curl -X GET \
  -H "X-Parse-Application-Id: xxxxxxxxx" \
  -H "X-Parse-REST-API-Key: xxxxxxxxx" \
  -H "X-Parse-Master-Key: xxxxxxxxx" \
  -G \
  --data-urlencode 'where={"gamelID":"afcd60f2-f9b2-4c5f-9148-1de46713552b"}' \
  https://xxxxxx.herokuapp.com/parse/classes/UserTag

Yesterday I tried to get the same data returned from CURL via PHP w/o any luck see #124

Today I wanted to see if I could get it working with a simple JS query see code below.

<!doctype html>
<head>
  <meta charset="utf-8">
  <title>My Parse App</title>
  <meta name="description" content="My Parse App">
  <meta name="viewport" content="width=device-width">
  <link rel="stylesheet" href="css/reset.css">
  <link rel="stylesheet" href="css/styles.css">
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script type="text/javascript" src="http://www.parsecdn.com/js/parse-latest.js"></script>
</head>
<body>
  <div id="main"></div>
  <script type="text/javascript">

    Parse.initialize("xxxxxx", "xxxxxx");
    Parse.serverURL = 'https://xxxx.herokuapp.com/parse'

    var UserTag = Parse.Object.extend("UserTag");
    var query = new Parse.Query(UserTag);
    query.equalTo("gameID", "afcd60f2-f9b2-4c5f-9148-1de46713552b");
    query.find({
      success: function(results) {
        console.log("Successfully retrieved " + results.length + " scores.");
        // Do something with the returned Parse.Object values
        for (var i = 0; i < results.length; i++) {
          var object = results[i];
          console.log(object.id + ' - ' + object.get('placeName'));
        }
      },
      error: function(error) {
        console.log("Error: " + error.code + " " + error.message);
      }
    })

  </script>
</body>
</html>

In the console log im getting a 403

image

I have verbose enabled but the Heroku logs really dont tell me much

2016-02-11T00:07:36.599825+00:00 heroku[router]: at=info method=POST path="/parse/classes/UserTag" host=xxxx.herokuapp.com request_id=b6c443c9-0a33-4e08-b309-4add49b9be18 fwd="12.215.39.106" dyno=web.1 connect=1ms service=17ms status=403 bytes=273

Anyone have any ideas..

I have double checked my keys, and again as stated above the CURL request works.

In the migration docs it states for using the JS SKD use this code

Parse.initialize("YOUR_APP_ID", "YOUR_APP_CLIENT_KEY");
Parse.serverURL = 'http://localhost:1337/parse'

Any thoughts? If its a silly mistake on my part I'm sure im not the only one running into it so any debugging would be appreciated.

@rogerhu
Copy link
Contributor

rogerhu commented Feb 11, 2016

The JavaScript SDK uses javascriptKey. The parse-server-example doesn't set it on init. You have to modify your index.js file:

https://github.com/ParsePlatform/parse-server-example/blob/master/index.js#L13-L17

var api = new ParseServer({
  databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
  cloud: process.env.CLOUD_COD
  javascriptKey: process.env.CLIENT_KEY || 'javascriptKey',

Shouldn't parse-server-example simply init it for backwards compatibility?

Also, I was able to get stuff to work with master Key but it becomes something like the following (for some reason I have to set useMasterKey on the actual save as well):

    Parse.initialize("myAppId", "javascriptKey");
    Parse.masterKey = "abc";
    Parse._useMasterKey = true;

   var testObject = new TestObject();
      testObject.save({
        foo: "bar",
        success: function (object) {
          $(".success").show();
        },
        error: function (model, error) {
          $(".error").show();
        }
      }); {useMasterKey: true});

@gfosco
Copy link
Contributor

gfosco commented Feb 11, 2016

This could be a keys issue. The best troubleshooting step here is not to set any client keys. No rest/js/client/dotnet... Only master key should be defined. Unauthorized means either you've hit the wrong server, or you're using the wrong appid/keys, or you've set at least 1 client key but not the one you're calling with (i.e. you set the rest key, and now get fails on JS).

@rogerhu
Copy link
Contributor

rogerhu commented Feb 11, 2016

@gfosco - it is definitely a keys issue, has the Parse JavaScript SDK been updated though to make things more streamlined to work with the master key? See comment above.

@gateway
Copy link
Author

gateway commented Feb 11, 2016

as @rogerhu pointed out the JS key works now.. thanks btw.. I think using a master key in JS web code would be a super bad thing personally so I try to avoid using the master key in 99% of the cases.

Now to try to figure out the issue #124 on the PHP side of things.

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

4 participants