- Python Programming Blueprints
- Daniel Furtado Marcus Pennington
- 289字
- 2021-06-24 18:53:52
Performing authentication
In this section, we are going to create the program that will perform authentication for us so we can use the Twitter API. We are going to do that using a simple Flask application that will expose two routes. The first is the root /, which will just load and render a simple HTML template with a button that will redirect us to the Twitter authentication dialog.
The second route that we are going to create is /callback. Remember when we specified the callback URL in the Twitter app configuration? This is the route that will be called after we authorize the app. It will return an authorization token that will be used to perform requests to the Twitter API. So let's get right into it!
Before we start implementing the Flask app, we need to add another model to our model's module. This model will represent the request authorization data. Open the models.py file in twittervotes/core/models and add the following code:
RequestToken = namedtuple('RequestToken', ['oauth_token',
'oauth_token_secret',
'oauth_callback_confirmed'])
This will create a namedtuple called RequestToken with the fields oauth_token, oauth_token_secret, and outh_callback_confirmed; this data will be necessary for us to perform the second step of the authentication.
Lastly, open the __init__.py file in the twittervotes/core/models directory and let's import the RequestToken namedtuple that we just created, as follows:
from .models import RequestToken
Now that we have the model in place, let's start creating the Flask application. Let's add a very simple template to show a button that will start the authentication process.
Create a new directory in the twittervotes directory called templates and create a file called index.html with the following content:
<html>
<head>
</head>
<body>
<a href="{{link}}"> Click here to authorize </a>
</body>
</html>
- 數據通信網絡實踐:基礎知識與交換機技術
- SEO 20日
- Oracle SOA Suite 11g Performance Tuning Cookbook
- Building RESTful Web Services with Spring 5(Second Edition)
- 電力物聯網工程技術原理與應用
- 網管工具使用與技巧大全
- Hands-On Microservices with Node.js
- INSTANT LinkedIn Customization How-to
- 圖神經網絡前沿
- Laravel Application Development Cookbook
- 新媒體交互藝術
- 互聯網戰略變革與未來
- ElasticSearch Server
- 企業網絡組建與維護項目式教程
- OpenShift Cookbook