session value state is missing

120 Views Asked by At

i want to make google oauth and when google redirect me with get req state and code then when i want to post it my djoser url in django rest i got non-field-errors :

session value state is missing 400 bad request to API_URL = 'https://7bfb-31-56-195-93.ngrok-free.app/auth/o/google-oauth2/'

class RedirectSocial(APIView):

    def get(self, request, *args, **kwargs):
        code = request.GET.get('code', '')
        state = request.GET.get('state', '')
        json_obj = {'code': code, 'state': state}
        
        print(json_obj)
        header = {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
        query_params = urllib.parse.urlencode(json_obj)
        print(query_params)
        form_data = {
        'code': code,
        'state': state,
        'client_id': '855150297727-frj54btbp93i1g50tglt5klg7g1jvug6.apps.googleusercontent.com',
        'client_secret': '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',
        'redirect_uri': 'https://7bfb-31-56-195-93.ngrok-free.app/google',
        'grant_type': 'authorization_code',
        }`

# Make the POST request using the requests library
      `  api_url = f"{API_URL}?{query_params}"  # Replace this with your actual API URL
        response = requests.post(API_URL, data=form_data )

        # Assuming the response contains the data you need
        response_data = response.json()
        print(response_data)
        # Return the JSON object as the response to the GET request
        return Response(json_obj)

i want to get access token from that end point

1

There are 1 best solutions below

1
AyushBhardwaj261 On

I also faced the same issue,I believe you might be running the frontend and backend seperately on different server.

Try to make build folder of frontend and use it as template with Backend to make it single server based application

For you reference I'm adding the hyperlink of the tutorial i followed : Link https://youtu.be/KiJFHBQ44sw?si=wup5aLrVK02dhDFH

OR:

Host you backend on Https and define these two configuration on settings.py

SESSION_COOKIE_SECURE = True

SESSION_COOKIE_SAMESITE="None"

Do let me know if this solution works for you.