![]() |
VOOZH | about |
In this article, we are going to build a flask application that will use the OAuth protocol to get user information. First, we need to understand the OAuth protocol and its procedure.
OAuth stands for Open Authorization and was implemented to achieve a connection between online services. The OAuth Community Site defines it as “An open protocol to allow secure authorization in a simple and standard method from web, mobile and desktop applications.”. A popular example of OAuth would be the Sign in with Google button present on various websites. Here the website service connects with the google service to provide you with an easy option to authorize your resource to the desired service. There are two versions of OAuth OAuth1.0 and OAuth2.0 now.
Step 1: Register your application as a client on the provider website. You will receive the client credentials which include the client ID and client secret.
Step 2: The client application sends an authorization request to the provider’s authorization URL.
Step 3: The user authenticates themselves on the provider’s site and allows resources to be used by the client service.
Step 4: The provider sents the authorization code to the client
Step 5: The client sends the authorization code to the provider’s authorization server.
Step 6: The provider sends the client tokens which can be used for accessing user resources.
Now that the concept of OAuth is clear we can start building our application. There are various libraries available to us that can be used to achieve OAuth. The library we will be using is AuthLib which supports OAuth 1.0 and OAuth 2.0.
To install the required dependencies type the below command in the terminal.
pip install -U Flask Authlib requests
Note: It is recommended to create a virtual environment before installing these dependencies.
The client credentials can be used directly in the program but in actual production, these credentials are to be stored in environment variables.
Create a folder called templates and inside create an index.html file. Paste the following code inside the index.html file. It is a simple code that creates buttons for every provider.
Let's create a simple flask application that will do nothing and will simply render the above-created HTML file onto the home page.
Run the server using the following command to make sure that the application is running successfully and the index.html page is displayed.
After creating the apps let's see how to add the Oauth for google, Twitter, and Facebook. But at first let's initialize the OAuth.
Here we have initialized the OAuth using the OAuth(app) class and we have changed our server name to localhost:5000. Now let's see how to create the OAuth for different Platforms.
Start server with:
python app.py
Then visit:
http://localhost:5000/👁 Image
Note: The OAuth configuration for every provider is different and depends on the version of OAuth. Every provider has its own documentation on implementing the protocol so make sure to check it out.