我在我的项目中使用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用户端。
由服务器返回的错误信息是:
供参考: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)
任何人有一个想法?
你会得到任何错误?我不认为问题中有足够的信息能够帮助你。 – diagonalbatman
@diagonalbatman我添加了从服务器端收到的答案。 – Mathieu
你是否正在返回一个特定的错误?我认为我们需要看到更多的代码。 – diagonalbatman