简体   繁体   English

Django Rest Framework API如何从请求中提取access_token?

[英]Django Rest Framework API How to extract access_token from request?

My API is currently protected by the OAuth2TokenAuthentication from django-oauth-toolkit, so that it can validate API requests that contains access token in following ways: 我的API当前受到django-oauth-toolkit中的OAuth2TokenAuthentication的保护,因此它可以通过以下方式验证包含访问令牌的API请求:

  1. as query param ?access_token=xxxx 作为查询参数?access_token=xxxx
  2. in header Authorization: Bearer xxxx 在标题Authorization: Bearer xxxx

while I can hardcode in my API view to try to get the access token from those 2 places, is there a canonical way to obtain the token? 虽然我可以在API视图中进行硬编码以尝试从这两个位置获取访问令牌,但是有没有一种规范的方法来获取令牌?

I dug through the code inside OAuth2TokenAuthentication, and borrowed it into my API View: 我翻阅了OAuth2TokenAuthentication内部的代码,并将其借入了我的API视图:

class IntrospectView(APIView):
    """
    An API view that introspect a given token
    """
    serializer_class = TokenIntrospectSerializer
    authentication_classes = []
    permission_classes = []

    def get(self, request, *args, **kwargs):
        oauthlib_core = get_oauthlib_core()
        valid, r = oauthlib_core.verify_request(request, scopes=[])
        if not valid:
            raise APIException('Invalid token')
        return Response(TokenIntrospectSerializer(r.access_token).data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 django rest 框架访问 jwt 令牌到 ZD18B8624A0F5F721DA7B82365FC56 - How to access a jwt token from a django rest framework to angular frontend 使用Django和REST-api获取salesforce.com access_token - Get salesforce.com access_token with django and REST-api 如何在 Django 的单个 API 中获取访问令牌和刷新令牌(rest_framework_jwt) - How to get access token and refresh token ( rest_framework_jwt ) in a single API in Django 在django-rest-auth中将`key`重命名为`access_token`吗? - rename `key` to `access_token` in django-rest-auth? 请求当前用户API令牌Django REST Framework - Request current users API token Django REST Framework 使用Javascript访问Django Rest Framework API-获取令牌 - Access Django Rest Framework API using Javascript - getting a Token 如何将令牌认证添加到使用Django REST框架创建的REST API - How to add token authentication to REST API created with Django REST framework Django rest 框架如何在nextjs post请求中传递授权令牌? - Django rest framework how to pass authorizing token in nextjs post request? 如何在 django rest 框架中传递用户令牌进行 API 测试? - How to pass the user token for API testing in django rest framework? 使用appid和访问令牌对Django rest框架进行身份验证? - authenticate django rest framework with appid and access token?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM