0

I'm trying to add social login to my django-rest-framework app, but I'm stuck on this problem and need some help.

Login Flow: Request code(GET) -> Response -> Request token(POST)(This part is where I'm stuck) -> Response

API reference here

So, after I log in to social account,(e.g. Facebook)click authorize my app button, I get access code like this :

@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

After that, I should ask for token, using user_code I got from above.

According to the API document, I should use POST method like this.

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}'

So I implemented my code like the following :

@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('/')

But, I get this error at request.POST(...) line.

'QueryDict' object is not callable

I just don't know how to solve this issue at request.POST(). Any help would be really appreciated.

1
  • 1
    request.POST is not callable., it implements dict., which has the values posted in endpoint or url Commented May 24, 2019 at 5:14

1 Answer 1

2

You can use a Third Party Library called requests to call API which resides outside of Django Project:

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.