0

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

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449

1 Answers1

0

I also faced the same issue,I believe you might be running the frontend and backend on different server. Try to make it single server based application i.e., merge the Frontend static files with Backend templates (i.e., in settings.py)

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

Do let me know if this solution works for you.

Ayush
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 29 '23 at 02:42