【问题标题】:How to access OAuth2 client_id in django rest framework?如何在 django rest 框架中访问 OAuth2 client_id?
【发布时间】:2014-06-29 07:28:47
【问题描述】:

我的 django rest 框架 API 受 Oauth2 工具包保护,但我不知道如何获取当前授权请求的 client_id

class RequestTransactionView(APIView):

    def post(self, request, format=None):
        transaction = self.parse_dictionary(request.DATA)
        return Response(str(transaction.goid))

我检查了request 对象,它给出了:

['DATA', 'FILES', 'QUERY_PARAMS', '_CONTENTTYPE_PARAM', '_CONTENT_PARAM', '_METHOD_PARAM', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_auth', '_authenticate', '_authenticator', '_content_type', '_data', '_default_negotiator', '_files', '_load_data_and_files', '_load_method_and_content_type', '_load_stream', '_method', '_not_authenticated', '_parse', '_perform_form_overloading', '_request', '_stream', '_user', 'accepted_media_type', 'accepted_renderer', 'auth', 'authenticators', 'content_type', 'method', 'negotiator', 'parser_context', 'parsers', 'stream', 'successful_authenticator', 'user']

然后我检查了我得到的successful_authenticator

['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'authenticate', 'authenticate_header', 'www_authenticate_realm']

我还检查了其他明显的提示,但没有运气。

【问题讨论】:

    标签: django-rest-framework oauth2-toolkit


    【解决方案1】:

    这比我想象的要容易,当我打印出request.auth时,我得到了一个字符串,我以为它是一个字符串类型,但后来我发现它是一个AccessToken,所以我可以从那里直接得到应用程序。

    print request.auth.application
    

    【讨论】:

      猜你喜欢
      • 2020-11-15
      • 2015-04-18
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 2022-01-20
      • 2017-07-24
      相关资源
      最近更新 更多