2011-08-02 190 views
0

我在我的项目中使用OAuth。但我已经进入认证问题。OAuth身份验证失败

它是我可以通过方法“POST”但不是方法“PUT”的Oauth的认证机制。 POST和PUT请求之间的唯一区别是方法类型。正文和标题是一样的。是我使用的请求如下:

POST

resp, cont = client.request("http://localhost:8000/api/1.0/booking/", 
          "POST", 
          data_booking, 
          headers=headers) 

PUT

resp, cont = client.request("http://localhost:8000/api/1.0/booking/", 
          "PUT", 
          data_booking, 
          headers=headers) 

客户端是OAuth用户端。

由服务器返回的错误信息是:

enter image description here

供参考:401未经授权

类似403禁止,但专为使用时的身份验证是可能的,但发生故障或尚未提供

我正在开发使用django框架。

请求方法如下:

def request(self, uri, method="GET", body=None, headers=None, 
     redirections=httplib2.DEFAULT_MAX_REDIRECTS, connection_type=None, 
     callback_url=None, realm=''): 
     DEFAULT_CONTENT_TYPE = 'application/x-www-form-urlencoded' 

     if not isinstance(headers, dict): 
      headers = {} 

     is_multipart = method == 'POST' and headers.get('Content-Type', 
      DEFAULT_CONTENT_TYPE) != DEFAULT_CONTENT_TYPE 

     if body and (method == "POST" or method == 'PUT') and not is_multipart: 
      parameters = dict(parse_qsl(body)) 
      if callback_url != None: 
       parameters['oauth_callback'] = callback_url 
     else: 
      if callback_url != None and not is_multipart: 
       parameters = {'oauth_callback': callback_url} 
      else: 
       parameters = None 

     req = Request.from_consumer_and_token(self.consumer, 
      token=self.token, http_method=method, http_url=uri, 
      parameters=parameters) 

     req.sign_request(self.method, self.consumer, self.token) 

     if method == "POST" or method == "PUT": 
      headers['Content-Type'] = headers.get('Content-Type', 
       DEFAULT_CONTENT_TYPE) 
      if is_multipart: 
       headers.update(req.to_header(realm)) 
      else: 
       body = req.to_postdata() 
     elif method == "GET": 
      uri = req.to_url() 
     else: 
      headers.update(req.to_header(realm)) 

     return httplib2.Http.request(self, uri, method=method, body=body, 
      headers=headers, redirections=redirections, 
      connection_type=connection_type) 

任何人有一个想法?

+0

你会得到任何错误?我不认为问题中有足够的信息能够帮助你。 – diagonalbatman

+0

@diagonalbatman我添加了从服务器端收到的答案。 – Mathieu

+0

你是否正在返回一个特定的错误?我认为我们需要看到更多的代码。 – diagonalbatman

回答

1

当HTTP方法为POST时,某些OAuth服务器实现仅在签名基本字符串中包含表单编码的主体参数。这是OAuth 1.0中的正确行为,但在后续修订中得到纠正。尝试做一个没有身体的PUT请求,看看是否有帮助。如果确实如此,则需要请求服务器库维护人员修复此问题,或者在使用put时将呼叫限制为不包含表单编码体。

+0

感谢您的回答。事实上,我已经使用python创建了一个oauth客户端。看来client.request只会在方法“post”时填充oauth参数。对于“放”,我手动做了这个,它的工作原理。奇怪!顺便说一句,谢谢你对oauth tuto。 – Mathieu