【问题标题】:django) request.POST method gives me errror : querydict object is not callabledjango) request.POST 方法给了我错误:querydict 对象不可调用
【发布时间】:2019-10-10 16:03:36
【问题描述】:

我正在尝试将社交登录添加到我的 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

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

根据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' 对象不可调用

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

【问题讨论】:

  • request.POST 是不可调用的。它实现了dict.,它的值发布在端点或url中

标签: django django-rest-framework django-views django-urls django-request


【解决方案1】:

您可以使用名为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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-10
    • 2015-02-15
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    相关资源
    最近更新 更多