Blog
Aug 16, 2014 - 1 MIN READ
HOW TO: authorize google oauth credentials through a Heroku environment

HOW TO: authorize google oauth credentials through a Heroku environment

Step-by-step guide to use Google APIs with OAuth credentials on Heroku without re-authentication. Python, OAuth2, refresh tokens, and environment variables.

My profile picture

Ryan Prater

  1. Take a look at heroku-config and install it.
  2. Create a file called ".env" in your project root directory and fill it with your environment variables. Make sure there's no spaces between your assignments.
    Example
  3. Make sure everything is running smooth with $ foreman start
  4. Implement the read_env() function shown here
  5. Create an instance of OAuth2WebServerFlow with access_type="offline" and approval_prompt='force'. This will grant you a refresh token and relieve you of having to re-auth every time your application runs.
  6. Run your application with typical OAuth storage shown here and copy your stored credentials into your CREDENTIALS variable in your .env file.
  7. Load your credentials like so: json.loads(os.environ.get('CREDENTIALS'))
  8. Construct an OAuth2Credentials instance
  9. Now your refresh token should prevent you from having to authorize and your credentials should be valid. You can now authorize and make API requests.
OAuth credentials instance
Copyright © 2025