I'm encountering an issue when trying to access the 'kakao' provider in Django-allauth. (Actually, google has a version issue now so, I want to figure it out kakao first) I have an exception for MultipleObjectsReturned or ObjectDoesNotExist. I have followed the documentation and tried various troubleshooting steps, but the problem persists.
Here is the code from my views.py file:
def kakao_callback(request): code = request.GET.get("code") print(f"code : {code}") # ---- Access Token Request ---- token_req = requests.get( f"https://kauth.kakao.com/oauth/token?grant_type=authorization_code&client_id={KAKAO_REST_API_KEY}&redirect_uri={KAKAO_REDIRECT_URI}&code={code}" ) token_req_json = token_req.json() error = token_req_json.get("error", None) if error is not None: raise JSONDecodeError(f"Failed to decode JSON: {error}", '{"error": "your_error_message"}', 0) access_token = token_req_json.get("access_token") print(f"access_token : {access_token}") # ---- Email Request ---- profile_request = requests.post("https://kapi.kakao.com/v2/user/me", headers={"Authorization": f"Bearer {access_token}"}, ) profile_json = profile_request.json() error = profile_json.get("error", None) if error is not None: raise JSONDecodeError("Failed to decode JSON", '{"error": "your_error_message"}', 0) kakao_account = profile_json.get("kakao_account") email = kakao_account.get("email", None) profile = kakao_account.get("profile") nickname = profile.get("nickname") profile_image = profile.get("thumbnail_image_url") # print(email) # ---- Signup or Signin Request ---- try: user = User.objects.get(email=email) social_user = SocialAccount.objects.get(user=user) if social_user is None: return JsonResponse( {"err_msg": "email exists but not kakao social user"}, status=status.HTTP_400_BAD_REQUEST, ) if social_user.provider != "kakao": return JsonResponse( {"err_msg": "no matching social type"}, status=status.HTTP_400_BAD_REQUEST, ) data = {"access_token": access_token, "code": code} accept = requests.post(f"{BASE_URL}accounts/kakao/login/finish/", data=data) accept_status = accept.status_code if accept_status != 200: print(f"Accept request status code: {accept.status_code}") return JsonResponse({"err_msg": "failed to signin_registered user."}, status=accept_status) accept_json = accept.json() accept_json.pop('user', None) refresh_token = accept.headers['Set-Cookie'] refresh_token = refresh_token.replace('=',';').replace(',',';').split(';') token_index = refresh_token.index(' refresh_token') cookie_max_age = 3600 * 24 * 14 # 14 days refresh_token = refresh_token[token_index+1] response_cookie = JsonResponse(accept_json) response_cookie.set_cookie('refresh_token', refresh_token, max_age=cookie_max_age, httponly=True, samesite='Lax') return response_cookie except User.DoesNotExist: cookie_max_age = 3600 * 24 * 14 data = {"access_token": access_token, "code": code} accept = requests.post(f"{BASE_URL}accounts/kakao/login/finish/", data=data) accept_status = accept.status_code if accept_status != 200: print(f"Failed to signup_new user. Status code: {accept_status}") return JsonResponse({"err_msg": "failed to signup_new user"}, status=accept_status) accept_json = accept.json() accept_json.pop('user', None) refresh_token = accept.headers['Set-Cookie'] refresh_token = refresh_token.replace('=',';').replace(',',';').split(';') token_index = refresh_token.index(' refresh_token') refresh_token = refresh_token[token_index+1] response_cookie = JsonResponse(accept_json) response_cookie.set_cookie('refresh_token', refresh_token, max_age=cookie_max_age, httponly=True, samesite='Lax') return response_cookie except JSONDecodeError as e: print(f"JSONDecodeError: {e}") raise except User.MultipleObjectsReturned as e: print(f"MultipleObjectsReturned: {e}") return JsonResponse( {"err_msg": "MultipleObjectsReturned. Check logs for details."}, status=status.HTTP_500_INTERNAL_SERVER_ERROR, ) except: return JsonResponse({"MESSAGE":"KEY_ERROR"}, status=400)I have checked the database, and there are two entries for the 'kakao', and 'google' providers in the SocialApp table. The client_id is correctly set for this provider. Also, when I checked the SocialApp.objects.all() it came out <QuerySet [<SocialApp: Kakao>, <SocialApp: google>]>.
Here is the code of SOCIALACCOUNT_PROVIDERS from settings.py file:
SOCIALACCOUNT_PROVIDERS = {'kakao': {'APP': {'client_id': KAKAO_SECRET_KEY,'secret': KAKAO_REST_API_KEY, } },'google': {'APP':{'clinet_id':secrets['SOCIAL_AUTH_GOOGLE_CLIENT_ID'],'secret': secrets['SOCIAL_AUTH_GOOGLE_SECRET'] }, }}Tracelog:
File "C:\Users\user\anaconda3\envs\Pj\Lib\site-packages\allauth\socialaccount\adapter.py", line 290, in get_app raise MultipleObjectsReturneddjango.core.exceptions.MultipleObjectsReturned