0

我在我的项目中使用OAuth 。但我遇到了身份验证问题。

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

邮政

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

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

客户端是 OAuth 客户端。

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

在此处输入图像描述

仅供参考:401未经授权

类似于 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)

有人有想法吗?

4

1 回答 1

1

当 HTTP 方法为 POST 时,某些 OAuth 服务器实现仅在签名基本字符串中包含表单编码的正文参数。这是 OAuth 1.0 中的正确行为,但在后来的修订中得到了纠正。尝试在没有正文的情况下发出 PUT 请求,看看是否有帮助。如果是这样,您将需要请求服务器库维护者解决此问题或限制您的调用在使用 put 时不包含表单编码的主体。

于 2011-08-02T16:00:01.620 回答