0

I am fairly inexperienced with user authentication especially through restful apis. I am trying to use python to log in with a user that is set up in parse.com. The following is the code I have:

  API_LOGIN_ROOT = 'https://api.parse.com/1/login'
  params = {'username':username,'password':password}
  encodedParams = urllib.urlencode(params)

  url = API_LOGIN_ROOT + "?" + encodedParams

  request = urllib2.Request(url)

  request.add_header('Content-type', 'application/x-www-form-urlencoded')

  # we could use urllib2's authentication system, but it seems like overkill for this
  auth_header =  "Basic %s" % base64.b64encode('%s:%s' % (APPLICATION_ID, MASTER_KEY))
  request.add_header('Authorization', auth_header)
  request.add_header('X-Parse-Application-Id', APPLICATION_ID)
  request.add_header('X-Parse-REST-API-Key', MASTER_KEY)

  request.get_method = lambda: http_verb


  # TODO: add error handling for server response
  response = urllib2.urlopen(request)
  #response_body = response.read()
  #response_dict = json.loads(response_body)

This is a modification of an open source library used to access the parse rest interface.

I get the following error:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/_webapp25.py", line 703, in __call__
    handler.post(*groups)
  File "/Users/nazbot/src/PantryPal_AppEngine/fridgepal.py", line 464, in post
    url = user.login()
  File "/Users/nazbot/src/PantryPal_AppEngine/fridgepal.py", line 313, in login
    url = self._executeCall(self.username, self.password, 'GET', data)
  File "/Users/nazbot/src/PantryPal_AppEngine/fridgepal.py", line 292, in _executeCall
    response = urllib2.urlopen(request)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found

Can someone point me to where I am screwing up? I'm not quite sure why I'm getting a 404 instead of an access denied or some other issue.

2
  • You might want to try using kennethreitz.com/requests-python-http-module.html instead of urlib2. Start watching this video at the 5:00 mark. It may have some clues as to what is wrong with your code: youtu.be/Q1pe6lHZeNs Commented Apr 19, 2012 at 21:13
  • You don't need the Authorization header with REST (nor is this the correct format) and you shouldn't be using your master key. Use the REST API key to limit the access of your request. Python isn't my strongest language anymore, but the docs seem to suggest that urlencoded params in the form of x-www-form-encoded should just be part of data. Try dropping the "Content-type"[sic] header and instantiating request with urllib2.Request(API_LOGIN_ROOT, encodedParams) Commented Apr 25, 2012 at 17:23

2 Answers 2

1

Make sure the "User" class was created on Parse.com as a special user class. When you are adding the class, make sure to change the Class Type to "User" instead of "Custom". A little user head icon will show up next to the class name on the left hand side.

This stumped me for a long time until Matt from the Parse team showed me the problem.

Sign up to request clarification or add additional context in comments.

Comments

0

Please change: API_LOGIN_ROOT = 'https://api.parse.com/1/login' to the following: API_LOGIN_ROOT = 'https://api.parse.com/1/login**/**'

I had the same problem using PHP, adding the / at the end fixed the 404 error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.