django) request.POST 方法给我错误:querydict 对象不可调用

django) request.POST method gives me errror : querydict object is not callable

我正在尝试将社交登录添加到我的 django-rest-framework 应用程序,但我遇到了这个问题,需要一些帮助。

登录流程:请求代码(GET)-> 响应-> 请求令牌(POST)(这部分是我卡住的地方)-> 响应

API reference here

因此,在我登录社交帐户(例如 Facebook)后,单击授权我的应用程序按钮,我将获得如下访问代码:

@api_view(['GET', 'POST'])
def kakao_login(request):
    # Extracting 'code' from received url
    my_code = request.GET["code"]
    request.session['my_code'] = my_code
    return HttpResponseRedirect(reverse('kakao_auth_code'))
    # This makes me redirect to 'kakao_auth_code' url

在那之后,我应该要求token,使用我从上面得到的user_code。

根据API document,我应该使用POST这样的方法。

curl -v -X POST https://kauth.kakao.com/oauth/token \
 -d 'grant_type=authorization_code' \
 -d 'client_id={app_key}' \
 -d 'redirect_uri={redirect_uri}' \
 -d 'code={authorize_code}'

所以我实现了如下代码:

@api_view(['GET', 'POST'])
def kakao_auth_code(request):
    my_code = request.session.get('my_code')
    try:
        del request.session['my_code']
    except KeyError:
        pass
    request.POST(grant_type = 'authorization_code', client_id = '428122a9ab5aa0e8    140ab61eb8dde36c', redirect_uri = 'accounts/kakao/login/callback/', code = my_code)
    return HttpResponseRedirect('/')

但是,我在 request.POST(...) 行收到此错误。

'QueryDict' object is not callable

我只是不知道如何在 request.POST() 解决这个问题。非常感谢任何帮助。

您可以使用名为 requests 的第三方库来调用位于 Django 项目外部的 API:

import requests

def kakao_auth_code(request):
   ...

   data = dict(grant_type = 'authorization_code', client_id = '428122a9ab5aa0e8    140ab61eb8dde36c', redirect_uri = 'accounts/kakao/login/callback/', code = my_code)
   response = requests.post('https://kauth.kakao.com/oauth/token', data=data)
   if response.status_code == 200:
      token = response.json().get('access_token')