title: Google API OAuth 2.0 can't authenticate Error 400: redirect_uri_mismatch
body: <p>I want to create an application that will have read only access to multiple users calendar events. However, I constantly fail to pass through authentication step. Browser opens new consent screen with <strong>Error 400: redirect_uri_mismatch</strong>. Here is what i did:</p>
  <p>For this I set up my project in google console. I did following:</p>
  <ol>
  <li>Start by creating a new project, and enabled the Google Calendar API on the “APIs” page.</li>
  <li>Configured consent screen</li>
  <li>Created Credentials via selecting Web Application for application type and OAuth 2.0 for authentication method, and set “Authorized redirect URIs” to &quot;http://localhost:8080&quot;. Right now web app is hosted locally.</li>
  <li>Downloaded credentials.json file locally. json file is below:</li>
  </ol>
  <pre><code>{&quot;web&quot;:
      {
          &quot;client_id&quot;:&quot;123234-jjjjj.apps.googleusercontent.com&quot;,
          &quot;project_id&quot;:&quot;ppppp-1234&quot;,
          &quot;auth_uri&quot;:&quot;https://accounts.google.com/o/oauth2/auth&quot;,
          &quot;token_uri&quot;:&quot;https://oauth2.googleapis.com/token&quot;,
          &quot;auth_provider_x509_cert_url&quot;:&quot;https://www.googleapis.com/oauth2/v1/certs&quot;,
          &quot;client_secret&quot;:&quot;zzzzzzzzzzzzzzzzzz&quot;,
          &quot;redirect_uris&quot;:[&quot;http://localhost:8080&quot;],
          &quot;javascript_origins&quot;:[&quot;http://localhost&quot;]
      }
  }
  </code></pre>
  <p>I use following code to call google API:</p>
  <pre><code>import pickle
  import os
  from google_auth_oauthlib.flow import Flow, InstalledAppFlow
  from googleapiclient.discovery import build
  from googleapiclient.http import MediaFileUpload
  from google.auth.transport.requests import Request
  
  def Create_Service(client_secret_file, api_name, api_version, *scopes):
      CLIENT_SECRET_FILE = client_secret_file
      API_SERVICE_NAME = api_name
      API_VERSION = api_version
      SCOPES = [scope for scope in scopes[0]]
      
      cred = None
  
      pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}.pickle'
      
      if os.path.exists(pickle_file):
          with open(pickle_file, 'rb') as token:
              cred = pickle.load(token)
  
      if not cred or not cred.valid:
          if cred and cred.expired and cred.refresh_token:
              cred.refresh(Request())
          else:
              flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
              cred = flow.run_local_server()
  
          with open(pickle_file, 'wb') as token:
              pickle.dump(cred, token)
  
      try:
          service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
          print(API_SERVICE_NAME, 'service created successfully')
          return service
      except Exception as e:
          print(e)
          return None
  
  
  CLIENT_SECRET_FILE = 'token.json'
  API_NAME = 'calendar'
  API_VERSION = 'v3'
  SCOPES = ['https://www.googleapis.com/auth/calendar.events.readonly']
  
  service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
  colors = service.colors().get().execute()
  
  print(colors)
  </code></pre>
  <p>When I run it, browser opens a consent screen with</p>
  <blockquote>
  <p><strong>Error 400: redirect_uri_mismatch</strong>.
  Then I follow http://localhost:8080/ and I see message &quot;The authentication flow has completed. You may close this window.&quot;</p>
  </blockquote>
  <p><a href="https://i.stack.imgur.com/oKYXE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oKYXE.png" alt="enter image description here" /></a></p>
  <p>And in the console I see following errors:</p>
  <pre><code> File &quot;main.py&quot;, line 48, in &lt;module&gt;
      service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
    File &quot;main.py&quot;, line 29, in Create_Service
      cred = flow.run_local_server()
    File &quot;/home/ev/Projects/TIM/web/.venv/lib/python3.8/site-packages/google_auth_oauthlib/flow.py&quot;, line 480, in run_local_server
      self.fetch_token(authorization_response=authorization_response)
    File &quot;/home/ev/Projects/TIM/web/.venv/lib/python3.8/site-packages/google_auth_oauthlib/flow.py&quot;, line 288, in fetch_token
      return self.oauth2session.fetch_token(self.client_config[&quot;token_uri&quot;], **kwargs)
    File &quot;/home/ev/Projects/TIM/web/.venv/lib/python3.8/site-packages/requests_oauthlib/oauth2_session.py&quot;, line 239, in fetch_token
      self._client.parse_request_uri_response(
    File &quot;/home/ev/Projects/TIM/web/.venv/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/clients/web_application.py&quot;, line 203, in parse_request_uri_response
      response = parse_authorization_code_response(uri, state=state)
    File &quot;/home/ev/Projects/TIM/web/.venv/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/parameters.py&quot;, line 268, in parse_authorization_code_response
      raise MismatchingStateError()
  oauthlib.oauth2.rfc6749.errors.MismatchingStateError: (mismatching_state) CSRF Warning! State not equal in request and response.
  </code></pre>
  <p>So, the question how to authenticate to google API via OAuth 2.0 in a correct way?</p>