title: Google oAuth 2.0 API Authentication Error: Error 400 - redirect_uri_mismatch (does not comply with policy) DJANGO APP
body: <p>I am trying to get my google authentication working on a Django app that is requesting Gmail and Calendar data. I have set up the oAuth API in the Google developer console and linked it with my project, and I've triple-checked that my redirect URI perfectly matches that in the code (No errors with HTTP vs. HTTPS nor any inconsistencies with the slashes). I made sure that my key, secret key, ClientID, and Client Secret are all configured and identical in my Django app's admin page. I have followed many youtube tutorials and searched other questions on stack overflow but Authentication is still not working. I am getting an Error 400: redirect_uri_mismatch. Even though I have checked many times to confirm that they are the same.</p>
  <p>From all the tutorials, I have learned that there are two main origins for this error:</p>
  <ol>
  <li>Server sided (can be fixed in the cloud hosting developer console)</li>
  <li>Client sided (can be fixed by altering the code)</li>
  </ol>
  <p>Both of these errors have their own individualized messages saying what type of mismatch it is.</p>
  <p>Mine, however, says this: You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy. \n\nIf you're the app developer, register the redirect URI in the Google Cloud Console.</p>
  <p>Here is a photo of the error
  [![Google Authentication error message][1]][1]</p>
  <pre><code>
  from django.shortcuts import render, redirect
  from django.http import HttpRequest
  from google_auth_oauthlib.flow import Flow
  from google.auth.transport.requests import Request
  from googleapiclient.discovery import build
  from .models import CredentialsModel
  from django.conf import settings
  from django.core.exceptions import ObjectDoesNotExist
  import os
  
  
  os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
  os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
  #Scopes are what we should be allowed to access
  SCOPES = ['https://mail.google.com/', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'openid']
  
  
  &quot;&quot;&quot;
  IF HAVING ISSUES WITH ANON USER:
  Make sure you are on 127.0.0.1:8000, not localhost, both from the test-page and
  the callback page. For some reason they are treated as different sessions and thus will have
  issues maintaining a logged in user
  &quot;&quot;&quot;
  
  def oauth2callback(request):
      activeUser = request.user
      #URL is what we need to use for authentication
      authorization_response = request.build_absolute_uri()
      flow = Flow.from_client_secrets_file(
              settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
              scopes=SCOPES,
  
              #This is where we are redirected after authentication
              redirect_uri='http://127.0.0.1:8000/google/oauth2callback')
      #Now get proper token
      flow.fetch_token(authorization_response = authorization_response)
      #print(request.user)
      #Now save in our database
      #print(flow.credentials)
      try :
          my_credential = CredentialsModel.objects.get(pk = activeUser)
      except ObjectDoesNotExist:
          CredentialsModel.objects.create(id = activeUser, credential = flow.credentials)
      else:
          my_credential.credential = flow.credentials
          my_credential.save()
  
      return redirect(flow.redirect_uri)     #activeUser.get_absolute_url())
  
  
    [1]: https://i.stack.imgur.com/2HXGP.png
  </code></pre>