Reputation: 377
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
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.
Upvotes: 0
Views: 1450
Reputation: 52018
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')
Upvotes: 2