OAuth - Testing locally or with Repl.it, without Heroku

So you followed the instructions from here, but when you got your Heroku app link, nothing shows up. Or you were wondering what the env.sh.sample file was or did in some of the example repos. Or you just wanted to know how to be able to run your webapp locally with OAuth.

In this article, we’ll go over how to set up your local workstation to be able to run your webapp locally with OAuth. In doing so, we can hopefully debug our webapp easier to be able to figure out why it’s not working properly on Heroku. In addition, we’ll cover how to have print statements in Flask, giving us another tool in debug toolbox.

Step 1: Registering the app with Github

Follow the instructions found here with a few changes.

  • If you're running this on Repl.it, make sure that your base URL is the link in the mini Repl.it browser window and not your heroku url.

  • If you're running this locally on your computer, make sure that http://127.0.0.1:portnumber is your base url and not your heroku url. Replace portnumber with a number (3000 or 5000 should work).

  • If you're running this locally (not on Repl.it), make sure that the links use http, and not https.

Step 2: Creating env.sh

Read here to find out how to securely put your Client ID and Client Secret (basically the username/password for your Github OAuth) into your webapp (The first part explains how to do this locally, and the last part explains how to do this on Heroku -- You've already done this on Heroku however, so you can skip this part). Ask a mentor for help if needed.

Step 3: Modifying webapp.py

We’re almost done! We have to modify webapp.py. Don’t forget to change these back when you’re ready for Heroku again. Find your login() method, and remove the https scheme. It should look like this:

@app.route('/login')

def login():

return github.authorize(callback=url_for('authorized', _external=True))


If you are running this code locally and not on Repl.it, at the very bottom of the file, update your app.run() with the parameter, port to match the port number that you chose when registering your application with Github (the port number is the number you chose from Step 1).

if __name__ == "__main__":

app.run(port=YOUR_PORT_NUMBER)

If it's on Repl.it, then it should like this:

if __name__=="__main__":

app.run(host='0.0.0.0')

Step 4: Ready to go!

You should be all set to host it locally now! Remember, every time you want to host it locally, run these commands in your project directory:

  1. . env.sh

  2. python webapp.py

Print Statements

Sometimes, when debugging, it is very helpful to insert print statements at certain points of the code to see where the program is running, what a certain value of a variable is, and give other useful information. You might want to include some print statements to help you debug.

At the very TOP of your file, include this import statement:

from __future__ import print_function

Now, wherever you want a print statement, you can use this line of code:

print("YOUR_MESSAGE", file=sys.stderr)

YOUR_MESSAGE will now be printed to your terminal to help you debug!