4

ApiKeyで認証されたPOSTリクエストをおいしいAPIに送信しようとしています

私のモデル:

class Thing(models.Model):
    name = models.TextField()

    def __unicode__(self):
        return u'%s'%self.name

私のModelResource

class ThingResource(ModelResource):
    class Meta:
        queryset = Thing.objects.all()
        resource_name="thing"
        authentication = ApiKeyAuthentication()
        authorization = DjangoAuthorization()

私のurls.py

from django.conf.urls.defaults import patterns, include, url

from tastypie.api import Api
from myapp.api import ThingResource

mobile_api = Api(api_name='mobile')
mobile_api.register(ThingResource())

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),

    (r'^api/', include(mobile_api.urls)),
)

と私のcURLコマンド

curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json"  -d"username=vikingosegundo" -d"api_key=12345" -X POST --data "{\"name\":\"arrrg\"}" http://localhost:8000/api/mobile/thing/

応答

{"error_message": "No JSON object could be decoded", 
 "traceback": "Traceback (most recent call last):\n\n  
    File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 178, in wrapper\n    response = callback(request, *args, **kwargs)\n\n
    File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 379, in dispatch_list\n    return self.dispatch('list', request, **kwargs)\n\n  
    File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 409, in dispatch\n    response = method(request, **kwargs)\n\n  
    File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 1077, in post_list\n    deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n  
    File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/resources.py\", line 328, in deserialize\n    deserialized = self._meta.serializer.deserialize(data, format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n
    File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/serializers.py\", line 161, in deserialize\n    deserialized = getattr(self, \"from_%s\" % desired_format)(content)\n\n  
    File \"/Users/vikingosegundo/Coding/Project/serverside/mysite/tastypie/serializers.py\", line 305, in from_json\n    return simplejson.loads(content)\n\n  
    File \"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py\", line 307, in loads\n    return _default_decoder.decode(s)\n\n  
    File \"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py\", line 319, in decode\n    obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n\n  
    File \"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py\", line 338, in raw_decode\n    raise ValueError(\"No JSON object could be decoded\")\n\n
    ValueError: No JSON object could be decoded\n"
}

私は何が間違っているのですか?tastypieをjsonオブジェクトにポイントするにはどうすればよいですか?auth+authが機能しているようです。

4

2 に答える 2

9

-dandを curl で使用すると--data、POST されたデータがマングリングされます。

usernameとparams を GET に含めるとapi_key、次のようにこれを解決できます。

    curl --dump-header - -H "Accept: application/json"\
    -H "Content-Type: application/json"  -X POST\
    --data "{\"name\":\"arrrg\"}"\
    "http://localhost:8000/api/mobile/thing/?username=vikingosegundo&api_key=12345"
于 2011-08-22T18:47:31.920 に答える
6

私は自分で答えを追加したい。Josh のソリューションの代わりに、資格情報を GETパラメーターとして追加するのではなく、カスタム HTTP ヘッダーとして送信します。

class CustomApiKeyAuthentication(ApiKeyAuthentication):
    def is_authenticated(self, request, **kwargs):
        username =  request.META.get('HTTP_X_MYAPP_USERNAME')   or request.GET.get('username')
        api_key =   request.META.get('HTTP_X_MYAPP_APIKEY')     or request.GET.get('apikey')

        if not username or not api_key:
            return self._unauthorized()
        try:
            user = User.objects.get(username=username)
        except (User.DoesNotExist, User.MultipleObjectsReturned):
            return self._unauthorized()
        request.user = user
        return self.get_key(user, api_key)


class ThingResource(MopedModelResource):
    creator     = fields.ForeignKey(UserResource, 'creator',    full = not True)
    class Meta:
        queryset        = Thing.objects.all()
        resource_name   = "thing"
        authentication  = CustomApiKeyAuthentication()
        authorization   = ThingAuthorization()

これで、リクエストにX-MYAPP-USERNAMEandを追加できます。 GET を使用した単純な telnet セッションX-MYAPP-APIKEY

GET /api/mobile/thing/ HTTP/1.1
X-MYAPP-APIKEY: 12345
X-MYAPP-USERNAME: vikingosegundo

HTTP/1.0 200 OK
Date: Wed, 24 Aug 2011 19:37:05 GMT
Server: WSGIServer/0.1 Python/2.7.1
Content-Type: application/json; charset=utf-8

{"meta": {"limit": 20, "next": null, …

および POST のセッション:

POST /api/mobile/thing/ HTTP/1.1
Content-Type: application/json
Content-Length: 49  
X-MYAPP-APIKEY: 12345
X-MYAPP-USERNAME: vikingosegundo

{"name":"qwerty","creator":"/api/mobile/user/1/"}
HTTP/1.0 201 CREATED
Date: Wed, 24 Aug 2011 20:12:32 GMT
Server: WSGIServer/0.1 Python/2.7.1
Content-Type: text/html; charset=utf-8
Location: http://1.0.0.127.in-addr.arpa:8000/api/mobile/thing/8/

また、ヘッダーが提供されていない場合に備えて GET パラメーターもチェックするため、これも機能します。

http://localhost:8000/api/mobile/thing/?username=vikingosegundo;apikey=12345;format=json
于 2011-08-24T20:17:34.267 に答える