title: How can I use Python Google API without getting a fresh auth code via browser each time?
body: <p>I am playing with the Google API. I am using <a href="https://developers.google.com/youtube/v3/docs/search/list" rel="noreferrer">this</a> as a starting point, <a href="https://pastebin.com/GHYtUdwM" rel="noreferrer">here</a> is the actual Python code.</p>
  
  <p>I have created a OAuth 2.0 client ID at <a href="https://console.developers.google.com/apis/credentials" rel="noreferrer">https://console.developers.google.com/apis/credentials</a> and downloaded it as <code>client_secret.json</code> which is used in the code as follows:</p>
  
  <pre><code>CLIENT_SECRETS_FILE = "client_secret.json"
  
  def get_authenticated_service():
    flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
    credentials = flow.run_console()
    return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
  </code></pre>
  
  <p>The contents of <code>client_secret.json</code> looks like this:</p>
  
  <pre><code>{
    "installed": {
      "client_id": "**REDACTED**",
      "project_id": "api-project-1014650230452",
      "auth_uri": "https:\/\/accounts.google.com\/o\/oauth2\/auth",
      "token_uri": "https:\/\/accounts.google.com\/o\/oauth2\/token",
      "auth_provider_x509_cert_url": "https:\/\/www.googleapis.com\/oauth2\/v1\/certs",
      "client_secret": "**REDACTED**",
      "redirect_uris": [
        "urn:ietf:wg:oauth:2.0:oob",
        "http:\/\/localhost"
      ]
    }
  }
  </code></pre>
  
  <p>The whole program works and successfully returns a meaningful data set, however, each time I run the program I am prompted as follows:</p>
  
  <blockquote>
  <pre><code>Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&amp;client_id=...
  Enter the authorization code:
  </code></pre>
  </blockquote>
  
  <p>I enter the code and the program works, but I have to visit this URL and get a fresh code each time the program runs. I was under the impression that <code>client_secret.json</code> existed precisely to prevent this from being necessary.</p>
  
  <p>What do I have to do to make my CLI Python program use the API without having to get a fresh token each time?</p>