Python 将参数放入django rest框架+;OAuth 1.0a

Python 将参数放入django rest框架+;OAuth 1.0a,python,django,rest,oauth,django-rest-framework,Python,Django,Rest,Oauth,Django Rest Framework,皮普冷冻 Django==1.6.2 Markdown==2.3.1 South==0.8.4 argparse==1.2.1 django-filter==0.7 django-guardian==1.1.1 django-oauth-plus==2.2.3 django-registration==1.0 djangorestframework==2.3.11 httplib2==0.8 ipdb==0.8 ipython==2.0.0 oauth2==1.5.211 psycopg2==2

皮普冷冻

Django==1.6.2
Markdown==2.3.1
South==0.8.4
argparse==1.2.1
django-filter==0.7
django-guardian==1.1.1
django-oauth-plus==2.2.3
django-registration==1.0
djangorestframework==2.3.11
httplib2==0.8
ipdb==0.8
ipython==2.0.0
oauth2==1.5.211
psycopg2==2.5.2
wsgiref==0.1.2
请求:

PUT /api/v1/place/14/ BODY: name=NEW3&latitude=55.74659&longitude=37.626484
答复:

{"detail": "Invalid signature. Expected signature base string: PUT&http%3A%2F%2Fyavezu.com%3A8005%2Fapi%2Fv1%2Fplace%2F14%2F&oauth_consumer_key%3Daa68ec89fc944c60880f59e18dc6e982%26oauth_nonce%3D-4587244018477714645%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1398769361%26oauth_token%3D03cd9df066244aa2884a5508ff0d9fff%26oauth_version%3D1.0"}
正如您所看到的,基本字符串中不包括PUT参数

我稍微挖掘了一段代码,发现了两个原因

  • oauth_提供程序中不支持PUT参数
  • 见第行:

    if request.method == "POST" and request.META.get('CONTENT_TYPE') == "application/x-www-form-urlencoded":
    
    oauth_provider.utils

    def get_oauth_request(request):
        """ Converts a Django request object into an `oauth2.Request` object. """
        # Django converts Authorization header in HTTP_AUTHORIZATION
        # Warning: it doesn't happen in tests but it's useful, do not remove!
        auth_header = {}
        if 'Authorization' in request.META:
            auth_header = {'Authorization': request.META['Authorization']}
        elif 'HTTP_AUTHORIZATION' in request.META:
            auth_header =  {'Authorization': request.META['HTTP_AUTHORIZATION']}
    
    
        # include POST parameters if content type is
        # 'application/x-www-form-urlencoded' and request
        # see: http://tools.ietf.org/html/rfc5849#section-3.4.1.3.1
        parameters = {}
    
        if request.method == "POST" and request.META.get('CONTENT_TYPE') == "application/x-www-form-urlencoded":
            parameters = dict((k, v.encode('utf-8')) for (k, v) in request.POST.iteritems())
    
        absolute_uri = request.build_absolute_uri(request.path)
    
        if "HTTP_X_FORWARDED_PROTO" in request.META:
            scheme = request.META["HTTP_X_FORWARDED_PROTO"]
            absolute_uri = urlunparse((scheme, ) + urlparse(absolute_uri)[1:])
    
        return oauth.Request.from_request(request.method,
            absolute_uri,
            headers=auth_header,
            parameters=parameters,
            query_string=request.META.get('QUERY_STRING', '')
        )
    
  • django.core.handlers.wsgi.WSGIRequest
    django.http.request.HttpRequest
    中不支持PUT参数:

    def_加载_post_和_文件(自): “”“如果内容类型是表单类型,则填充self.\u post和self.\u文件”“” 如果self.method!='邮政‘’ self.\u post,self.\u files=QueryDict(“”,encoding=self.\u encoding),多值dict() 回来

  • 我想知道Django REST框架如何能够在不支持PUT参数的情况下通过Django提供RESTful API?我做错了什么? POST请求工作正常

    我还问了一个关于谷歌集团的问题:

    根据

    我建议:

  • 而是将身份验证信息放在授权头中,django oauth plus也正确支持授权头
  • 向django oauth plus提交pull请求,添加对在非POST消息体中包含x-www-form-urlencoded oauth参数的支持

  • 无论哪种方式,问题都不是REST框架不支持解析PUT请求中的数据-它支持,并且request.data仍将正常填充。

    看起来django oauth plus的此修复程序应该有帮助:if request.META.get('CONTENT_TYPE',None)=“application/x-www-form-urlencoded”:if request.method='POST':parameters=request.POST else:parameters=getattr(request,'DATA',{})parameters=dict((k,v.encode('utf-8'))for(k,v)in parameters.iteritems())