あなたが考慮する必要がある2つのものがあります。認証と承認。
まず、リクエストメソッドがGETの場合はAPIキーに関係なくすべてのユーザーを認証する必要があります。他のすべてのメソッドではApiKeyAuthenticationを使用します。
ここで、認証されたすべてのユーザーは、承認の対象となります。ここでは、GET要求が常に許可されていることを確認する必要もあります。このような何かあなたが始める必要があります。
from tastypie.resources import ModelResource
from tastypie.authentication import ApiKeyAuthentication
from tastypie.authorization import DjangoAuthorization
class MyAuthentication(ApiKeyAuthentication):
"""
Authenticates everyone if the request is GET otherwise performs
ApiKeyAuthentication.
"""
def is_authenticated(self, request, **kwargs):
if request.method == 'GET':
return True
return super(MyAuthentication, self).is_authenticated(request, **kwargs)
class MyAuthorization(DjangoAuthorization)
"""
Authorizes every authenticated user to perform GET, for all others
performs DjangoAuthorization.
"""
def is_authorized(self, request, object=None):
if request.method == 'GET':
return True
else:
return super(MyAuthorization, self).is_authorized(request, object)
class MyResource(ModelResource):
class Meta:
authentication = MyAuthentication()
authorization = MyAuthorization()
だから、基本的ApiKeyAuthentication
とDjangoAuthorization
を使用するため、あなたのアプローチは、GETリクエストのための特別な治療を欠いていました。
出典
2012-09-05 01:33:24
kgr
詳細ソリューションをお寄せいただきありがとうございます。それはうまくいく。 DjangoAuthorizationはすでにGETリクエストを常に許可しているので、私はMyAuthorizationをスキップしました。 –
あなたは歓迎です:)私はうまく働いてうれしいです。 – kgr