2

私はdjango restフレームワークのトークン認証を使用しています。無効または既に削除されているトークン (トークン aesdghfhkjdsajgaadsa) を指定して URL を呼び出すと、ユーザー名とパスワードを要求するポップアップが表示されます。どうすればそのポップアップを回避できますか? 私はちょうど応答が必要です

{"status": -1, "errors": "Token Expired"}

与えられたカスタムトークン認証を使用していますが、

class ExpiringTokenAuthentication(TokenAuthentication):

def authenticate_credentials(self, key):
    try:
        token = self.model.objects.get(key=key)
    except self.model.DoesNotExist:
        raise exceptions.AuthenticationFailed('Invalid token')

    if not token.user.is_active:
        raise exceptions.AuthenticationFailed('User inactive or deleted')

    # This is required for the time comparison
    utc_now = datetime.utcnow()
    utc_now = utc_now.replace(tzinfo=pytz.utc)

    if token.created < utc_now - timedelta(hours=24):
        token.delete()
        raise exceptions.AuthenticationFailed('Token has expired')

    return token.user, token

これに対する解決策はありますか?

4

2 に答える 2

2

ポップアップは、HTTP Basic/Digest 認証スキームによって生成されたユーザー名/パスワードだと思いますか? これは、BasicAuthentication 認証クラスに由来する可能性が最も高いです。

APIView.authentication_classes でリストを明示的に指定しない限り、Django Rest Framework は DEFAULT_AUTHENTICATION_CLASSES にリストされている認証方法を繰り返します。

http://www.django-rest-framework.org/api-guide/authentication/#setting-the-authentication-scheme

于 2015-04-29T12:21:10.317 に答える