This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Authentication

What if you need to pass in your username and password to the API? Use the auth keyword argument.

Note: getpass() allows you to enter passwords without showing it.

from getpass import getpass
response = requests.get("https://api.github.com/user", auth=("my_username", getpass()))
print(response.status_code)  ## 401 if failed
print(response.json())

Try not to log in incorrectly too many times or you’ll end up with a 403 Forbidden status code (that’s what happened to me!) Or you can be a rebel and try it (but will have to wait for a while to log in again later)!

Note that this feature is actually deprecated for the Github REST API (it’s used more as an example). You will need to use a Personal Access Token if you want to use this for real in the future!