我在我的项目中使用 OAuth 。但我遇到了身份验证问题。
我可以通过“POST”方法传递Oauth的身份验证机制,但不能通过“PUT”方法传递。 POST和PUT请求之间的唯一区别是方法类型。身体和头部是一样的。我使用的请求如下:
发表
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客户端。
服务器返回的错误消息是:
Fyi:401 Unauthorized
类似于403 Forbidden,但特别适用于可能进行身份验证但已失败或尚未提供的情况
我正在使用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 :(得分:1)
当HTTP方法为POST时,某些OAuth服务器实现仅在签名基本字符串中包含表单编码的正文参数。这是OAuth 1.0中的正确行为,但在以后的修订版中已得到纠正。尝试在没有身体的情况下发出PUT请求,看看是否有帮助。如果是这样,您将需要请求服务器库维护人员修复此问题,或者在使用put时限制您的调用不包括表单编码的正文。