2012-04-19 70 views
0

我对用户身份验证相当不熟悉,特别是通过宁静的apis。我正在尝试使用python登录parse.com中设置的用户。以下是我的代码:Parse.com用户登录 - 404错误

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) 

这是用于访问解析休息界面的开源库的修改。

我得到以下错误:

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 

有人能指出我的地方我搞砸了?我不太清楚为什么我会得到404而不是拒绝访问或其他问题。

+0

您可能想尝试使用http://kennethreitz.com/requests-python-http-module.html而不是urlib2。从5:00开始观看此视频。它可能有一些关于你的代码有什么问题的线索:http://youtu.be/Q1pe6lHZeNs – alan 2012-04-19 21:13:08

+0

你不需要使用REST的授权标题(这也不是正确的格式),你不应该使用你的主密钥。使用REST API密钥来限制您的请求的访问。 Python不再是我最强大的语言,但文档似乎暗示x-www-form-encoded形式的urlencoded params应该只是数据的一部分。 尝试删除“Content-type”[sic]头并使用urllib2.Request实例化请求(API_LOGIN_ROOT,encodedParams) – 2012-04-25 17:23:09

回答

1

确保“用户”类是在Parse.com上创建的特殊用户类。在添加类时,请确保将类类型更改为“用户”而不是“自定义”。一个小小的用户头像图标将出现在左侧的课程名称旁边。

很长时间以来,我一直困扰着我,直到Parse团队的Matt向我展示了这个问题。

0

请更改:API_LOGIN_ROOT = 'https://api.parse.com/1/login'以下几点:API_LOGIN_ROOT = 'https://api.parse.com/1/login**/**'

我不得不使用PHP同样的问题,加入/末固定错误。