milesrichardson / ParsePy

A relatively up-to-date fork of ParsePy, the Python wrapper for the Parse.com API. Originally maintained by @dgrtwo
MIT License
516 stars 184 forks source link

Accessing ParseServer from Flask-Google App Engine #147

Closed SovcikJoe closed 8 years ago

SovcikJoe commented 8 years ago

So after testing out some functionality of Parse and this library, I decided to port my code that I used for playing around with Parse and use it directly inside a web app created in Flask, as my ultimate goal is to create a Webapp that will be used to add/edit/manage data in the ParseDatabase.

My code works perfectly when it's standalone, nothing changes. But when I run the same code from Flask, I get {'error':unathorized'} as if I was running the query from the browser, or something blocking me from logging into Parse.

The Flask code is here '`

@app.route('/parseinsert')
def run_parse_db_insert():
    """The method for testing implementation and design of the Parse Db
    """
    pc = ParseCommunication()
    print(pc.get_all_names_rating_table())
    return 'done'

The ParseCommunication is my Class that deals with Parse. If I run ParseCommunication from that script, with the same code as above in the main part, everything works perfectly.

The Parse Server I am using right now is LocalHosted. Everything is on LocalHost as I am just developing and testing. I run the Flask app with dev_appserver.py from Google App Engine.

milesrichardson commented 8 years ago

@SovcikJoe There are two possibilities

  1. Your code is connecting to parse.com, not your self hosted server, and therefore you are getting unauthorized
  2. Your code is connecting to your self hosted server, but one of the keys is wrong.

Given that your code "works perfectly when it's standalone", clearly there is something happening in the way that you import the ParseCommunication class such that you are losing either the environment variable configuring PARSE_API_ROOT, or the register method is not being called in scope.

What happens if you put all the parse initialization INSIDE the run_parse_db_insert() function? Literally copy and paste it into the function scope. Something like:

    @app.route('/parseinsert')
    def run_parse_db_insert():
        """The method for testing implementation and design of the Parse Db
        """
        import os
        os.environ["PARSE_API_ROOT"] = "http://your_server.com:1337/parse"

        # Everything else same as usual

        from parse_rest.datatypes import Function, Object, GeoPoint
        from parse_rest.connection import register
        from parse_rest.query import QueryResourceDoesNotExist
        from parse_rest.connection import ParseBatcher
        from parse_rest.core import ResourceRequestBadRequest, ParseError

        APPLICATION_ID = '...'
        REST_API_KEY = '...'
        MASTER_KEY = '...'

        register(APPLICATION_ID, REST_API_KEY, master_key=MASTER_KEY)

        # ...

        pc = ParseCommunication()
        print(pc.get_all_names_rating_table())
        return 'done'

If you can get that to work, then I'd look into how you're importing code.

I can't really debug this without seeing more.

SovcikJoe commented 8 years ago

@milesrichardson You are right,, that's the only thing that must be wrong though I am not completely sure how to identify what's causing the issue.

So what I did to try to identify the cause of the issue: I used the code below and had it in the stand-alone script. That worked perfectly, as expected. Then I added the same code into the run_parse_db_insert(). And I got the unathorized message again.

I kept the Query line of code in the views.py and then: I added the init code at the beginning of the views.py script or I added the init code into the application init script and run.py script

None of it worked

The code: `import os

os.environ["PARSE_API_ROOT"] = "http://127.0.0.1:1337/parse"

REST_API_KEY = 'test' APPLICATION_ID = '@@@' MASTER_KEY = '@@@@'

from parse_rest.datatypes import Function, Object, GeoPoint from parse_rest.connection import register from parse_rest.query import QueryResourceDoesNotExist from parse_rest.connection import ParseBatcher from parse_rest.core import ResourceRequestBadRequest, ParseError from parse_rest.user import User from models import ParseModels from parse_rest.connection import ParseBatcher

register(APPLICATION_ID, REST_API_KEY, master_key=MASTER_KEY)

name = ParseModels.Names.Query.get(name='Ludvig') print(name) `

Any other ideas of what I can do to debug this/try to solve this?

Or what kind of information would you need to be able to advise me where to look?

Thank you BTW. I realize this is not even probably related to the library but you are still helping me.

Or what I could do, if that would work, I could try to just use a remote parse server once I set it up somewhere and use that for development if that would work. But I have a feeling that if I can't make it run with local environment than I'd have the same problem with remote one.

Again, thank you for any advise

Joe