title: How to fix 504 Gateway Time-out while connecting to google calendar api
body: <p>I have created project which fetch one month events of all the calendars in my calendar list.
  To do this, I followed google calendar api documentation <a href="https://developers.google.com/calendar/api/quickstart/python" rel="nofollow noreferrer">Quickstart</a>.</p>
  <p>This is function which connect to google api.</p>
  <pre><code>def connect_google_api():
      creds = None
      if os.path.exists('token.json'):
          creds = Credentials.from_authorized_user_file('token.json')
      if not creds or not creds.valid:
          if creds and creds.expired and creds.refresh_token:
              creds.refresh(Request())
          else:
              flow = InstalledAppFlow.from_client_secrets_file('credentials.json',SCOPES)
              creds = flow.run_local_server(port=0)
          
          with open('token.json','w') as token:
              token.write(creds.to_json())
      service = build('calendar','v3',credentials=creds)
  
  </code></pre>
  <p>I hosted the website using Apache and all the pages are working except google calendar.
  I first thought that there might be some problem while reading credentials.json so I use this to find out</p>
  <pre><code>data = None
  with open('credentials.json') as f:
      data = json.load(f)
  qw = data
  
  </code></pre>
  <p>Website is still in debug mode so I made some error to check local variable and I found out that <code>data</code> and <code>qw</code> variable contains credentails.json.</p>
  <p>I think the problem is happening because of the below line</p>
  <pre><code>creds = flow.run_local_server(port=0)
  
  </code></pre>
  <p>I opened credentails.json and i found this <code>redirect_uris&quot;:[&quot;urn:ietf:wg:oauth:2.0:oob&quot;,&quot;http://localhost&quot;]</code></p>
  <p>I changed <code>http://localhost</code> to <code>http://test.example.com/</code>.
  I dont know what to put at the place of <code>http://localhost</code>.
  I created OAuth 2.0 Client IDs in google console and chose <code>Application Type</code> as <code>Desktop App</code> (Also tried with web application).</p>
  <p>I want to find out what changes are required in this code so that it start working on server. On localhost everything is working fine. Please help.</p>
  <p><strong><strong>Edit1</strong></strong></p>
  <p>I found out that <code>creds = flow.run_local_server(port=0)</code> works only for localhost and I can not find any thing from documentation about replacing this so that it works on Production.</p>
  <p><strong><strong>Edit2</strong></strong></p>
  <p>I checked server error log and I found that</p>
  <p><code>Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&amp;client_id=somethinvf.apps.googleusercontent.com&amp;redirect_uri=http%ivnf2Flocalhost%divnid%2F&amp;scope=https%inviF%2Fwww.googleapis.com%2Fauth%2Fcalendar&amp;state=something&amp;access_type=offline, referer: https:mywebsite.com/</code></p>
  <p>Why this is happening on my server console, I want this to happen on user's browser and why there is still localhost in redirect_uri?</p>
  <p>this is my json file.</p>
  <pre><code>{
    &quot;web&quot;:{
      &quot;client_id&quot;:&quot;something.apps.googleusercontent.com&quot;,
      &quot;project_id&quot;:&quot;&lt;project id&gt;&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;&lt;client_secret&gt;&quot;,
      &quot;redirect_uris&quot;:[
        &quot;https://mywebsite.com&quot;
      ]
    }
  }
  
  </code></pre>